0

My XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <Slider Name="sldLength" Margin="0 0 0 0"  Grid.Column="0" 
            Minimum="5" Maximum="30" SmallChange="1"  LargeChange="1"
            Value="10" ValueChanged="sldLength_ValueChanged"/>
    <TextBlock Name="tblLength" Margin="0 10 5 0" Text="10" Grid.Column="1"
            FontSize="{StaticResource PhoneFontSizeMediumLarge}"/>
</Grid>

My Code begind:

private void sldLength_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    tblLength.Text = e.NewValue.ToString();
}

2 problems:

  1. ValueChanged event fires when the application is loaded, this is not a problem, but for some reason at the time of the event firing tblLength is NULL and this throws an exception, I have wrapped it in an if statement checking for that NULL, but this surely should not be the issue right?
  2. Slider does not change in the increments of 1, even though SmallChange and LargeChange are set to 1. How could that be fixed?

Thnks for any help...

--EDIT--

Solved problem 1 with Chepene suggestion.
Solved problem 2 with little cheeky event handler:

private void sldLength_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    ((Slider)sender).Value = Math.Round(((Slider)sender).Value);
}
4

2 回答 2

1

您可以使用绑定。像这样:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <Slider Name="sldLength" Margin="0 0 0 0"  Grid.Column="0" 
            Minimum="5" Maximum="30" SmallChange="1"  LargeChange="1"
            Value="10"/>
    <TextBlock Name="tblLength" Margin="0 10 5 0" Text="{Binding ElemantName=sldLength, Path=Value}" Grid.Column="1"
               FontSize="{StaticResource PhoneFontSizeMediumLarge}"/>
</Grid>

这里 tblLength 从 sldLength 的值中获取其文本。

于 2013-06-13T11:14:31.983 回答
0

这对我来说非常有效。首先检查 Slider 的 OldValue,它是否为空。

private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
   if(slider.OldValue != null)
   {
      textbox1.Text = silder1.Value.ToString();
   }
}

希望这有帮助。

于 2015-11-20T11:01:39.123 回答