0

I am definig several TextBoxes with XAML, but the Textboxes do not behave similar although the code for me is exactly the same.`

... /* this is the definition of 4 TextBoxes which work as expected  */
    <TextBox  x:Name="TBXRed"    Text ="0" TextAlignment="Center" Grid.Column="2" Grid.Row="1" Margin="5,320,130,10"  TextChanged="redChanged"   />
    <TextBox  x:Name="TBXGreen"  Text ="0" TextAlignment="Center" Grid.Column="2" Grid.Row="1" Margin="45,320,90,10"  TextChanged="greenChanged" />
    <TextBox  x:Name="TBXBlue"   Text ="0" TextAlignment="Center" Grid.Column="2" Grid.Row="1" Margin="85,320,50,10"  TextChanged="blueChanged" />
    <TextBox  x:Name="TBXBright" Text ="0" TextAlignment="Center" Grid.Column="2" Grid.Row="1" Margin="125,320,10,10"  TextChanged="brightChanged" />

...

           <Label   x:Name="LBLRadius" Content="radius "  Grid.Column="2" Grid.Row="1" FontSize="13" FontWeight="DemiBold" Foreground="MediumBlue" Height="30" />
        <Slider   x:Name="sliderRadius"  Orientation="Horizontal"  Width="100" Height="20"  TickFrequency="1" Value="20"
                  ValueChanged="sliderRadiusChanged" Interval="1" Minimum="0"  Maximum="80"  />
        <TextBox x:Name="TBXRadius" Text="20" TextAlignment="Center" Width="30"  Height="20" TextChanged="radiusChanged" HorizontalAlignment="Center" VerticalAlignment="Center"  />

in the code behind file there is the handling for the TextChanged Event (one example).

    private void sliderRedChanged(object sender, fRoutedPropertyChangedEventArgs<double> e)
    {
        double newVal = e.NewValue;
        adjustValueDisplay(0, newVal);
        sliderRed.Value = newVal;
    }`
       private void redChanged(object sender, TextChangedEventArgs e)
    {
        redCor = TBXRed.Text;
        redCorI = Convert.ToInt32(redCor);
        sliderRed.Value =Convert.ToDouble(redCorI);
    }

    private void adjustValueDisplay(int aPos, double aValue)
    {
        switch (aPos)
        {
            case 0:
                redCor  = Convert.ToString(Math.Truncate(aValue));
                redCorI = Convert.ToInt32(redCor);
                TBXRed.TextChanged -= redChanged;
                TBXRed.Text = redCor;
                TBXRed.TextChanged += redChanged;
                ProjectParms.SetAsString("redCor", redCor);
                break;
                .......
            default:
                break;
        }
    }

This works very fine, but a little bit later I wont to define the parameters for an unsharp masking function

<StackPanel x:Name="SharpenVals" Grid.Row="3" Orientation="Horizontal" Grid.ColumnSpan="3" Grid.RowSpan="1" >
        <Label  Name="LBLSharpen"  Content="Sharpen:    on/off " FontSize="14" FontWeight="DemiBold" Foreground="MediumBlue" Height="30" Width="125" />
        <CheckBox x:Name="CBXDoSharpen"  Content="   " FontSize="13" FontWeight="DemiBold" Foreground="MediumBlue" Height="16" RenderTransformOrigin="-0.197,0.815" Width="30" />
        <Label   x:Name="LBLRadius" Content="radius "  Grid.Column="2" Grid.Row="1" FontSize="13" FontWeight="DemiBold" Foreground="MediumBlue" Height="30" />
        <Slider   x:Name="sliderRadius"  Orientation="Horizontal"  Width="100" Height="20"  TickFrequency="1" Value="20"
                  ValueChanged="sliderRadiusChanged" Interval="1" Minimum="0"  Maximum="80"  />
        <TextBox x:Name="TBXRadius" Text="20" TextAlignment="Center" Width="30"  Height="20" TextChanged="radiusChanged" HorizontalAlignment="Center" VerticalAlignment="Center"  />
        
        <Label   x:Name="LBLStrength" Content="  strength "  Grid.Column="2" Grid.Row="1" FontSize="13" FontWeight="DemiBold" Foreground="MediumBlue" Height="30" />
        <Slider   x:Name="sliderStrength"  Orientation="Horizontal"  Width="80" Height="20"  TickFrequency="1" Value="20"
                  ValueChanged="sliderStrengthChanged" Interval="1" Minimum="0"  Maximum="80"  />
        <TextBox Name="TBXStrength" Text="55" TextAlignment="Center" Width="30"  Height="20" TextChanged="strengthChanged"  />

        <Label  x:Name="LBLThreshold" Content="  threshold "  Grid.Column="2"  Grid.Row="1" FontSize="13" FontWeight="DemiBold" Height="30"
                 Foreground="MediumBlue"  />
        <TextBox x:Name="TBXThreshold" Text="2" TextAlignment="Center"  Width="30"  Height="20" TextChanged="thresholdChanged" />
        
        <Label  x:Name="LBLBrightLim" Content="  brightness limit  "  Grid.Column="2"  Grid.Row="1" FontSize="13" FontWeight="DemiBold" Height="30"
                 Foreground="MediumBlue"  />
        <TextBox Name="TBX_BrightLim" Text="200" TextAlignment="Center"  Width="30"  Height="20" TextChanged="thresholdChanged" />
    </StackPanel>


    private void sliderRadiusChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        double newVal = e.NewValue;
        adjustSharpeningDisplay(0, newVal);
        sliderRadius.Value = Math.Round(newVal);
    }
        public void radiusChanged(object sender, TextChangedEventArgs e)
    {
        radius = TBXRadius.Text;
        radiusI = Convert.ToInt32(radius);
        sliderRadius.Value = radiusI;
    }

Code behind section:

    private void sliderRadiusChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        double newVal = e.NewValue;
        adjustSharpeningDisplay(0, newVal);
        sliderRadius.Value = Math.Round(newVal);
    }
    public void radiusChanged(object sender, TextChangedEventArgs e)
    {
        radius = TBXRadius.Text;
        radiusI = Convert.ToInt32(radius);
        sliderRadius.Value = radiusI;
    }

        private void adjustSharpeningDisplay(int aPos, double aValue)
    {
        switch (aPos)
        {
            case 0:
                radius = Convert.ToString(Math.Truncate(aValue));
                radiusI = Convert.ToInt32(radius);
                TBXRadius.TextChanged -= radiusChanged;
                TBXRadius.Text = radius;
                TBXRadius.TextChanged += new System.Windows.Controls.TextChangedEventHandler(this.radiusChanged);
                ProjectParms.SetAsString("radius", radius);
                break;
      .......
            default:
                break;
        }
    }

and this, although it looks very the same for me, does not work.

I do get an System.NullReferenceException: "Object reference not set to an instance of an object." saying, that TBXRadius is null. (and this I do not understand)

Has anyone an idea why this two pieces of code behave different and why TBXRadius is not in the visible view.

The attached Image shows how the window should look !

Any recommendation is welcome.

Greetings Wolfgang

Clemens
  • 123,504
  • 12
  • 155
  • 268

0 Answers0