bubye
bubye

Reputation: 89

Silverlight Why doesn't this work

I am trying to create a slider(without binding). Currently i did this:

Xaml:

<Slider Height="68" HorizontalAlignment="Left" Margin="52,45,0,0" x:Name="slider1" VerticalAlignment="Top" Width="256" Minimum="1" Maximum="40" Value="10" ValueChanged="slider1_ValueChanged" />
    <TextBlock x:Name="textBlock1" Margin="52,120,0,0" Text="Slide it!" ></TextBlock>

And in my cs:

private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
  textBloxk1.FontSize = slider1.Value;
}

But the silverlight page keeps loading and won't show the slider, anyone know what I'm doing wrong??

Upvotes: 1

Views: 334

Answers (2)

Antonio Pelleriti
Antonio Pelleriti

Reputation: 859

Probably at first ValueChanged event, slider1 and textblock1 are still null. try this:

private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
            if (textBlock1 != null && slider1 != null)
            {
                textBlock1.FontSize = slider1.Value;
            }
}

Upvotes: 1

obenjiro
obenjiro

Reputation: 3750

look at your Xaml.. you setting value to 10 Value="10"... but at that time textBlock dosn't exist.. be carefull..

when parser parse Xaml it first create Slider then sets all values to slider (and fire all attached events), and only then it creates TextBlock...

so change you code to this, and everithing should be fine..

    private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        if (textBlock1 != null && slider1 != null)
        {
            textBlock1.FontSize = slider1.Value;
        }
    }

Upvotes: 0

Related Questions