Slider.Value doesn't reflect dragging of slider's Thumb element RRS feed

  • Question

  • Hi everybody !

    I want to bind the value of TextBlock's Text property to slider's Value property.

    Everything seems obvious : we need to use such syntax

                <Slider x:Name="slider" HorizontalAlignment="Left" Margin="521,300,0,0" VerticalAlignment="Top" Width="618"/>
                <TextBlock x:Name="sliderValueTextBlock" HorizontalAlignment="Left" Margin="779,475,0,0" Text="{Binding ElementName=slider, Path=Value}" VerticalAlignment="Top"/>

    But it goes interesting right after launching application : if I drag a slider's Thumb nothing actually happens. No changes are made to TextBlock's Text property value.

    More than that, I see that while dragging the Thumb, value of Thumb's ToolTip is equal 0 all the time.

    I've investigated further - tried TwoWay binding mode etc. but the only thing I achieved is that I can set the value of TextBlock's Text property equl to slider's Value only if I put this code into Slider's ValueChanged event handler :

    Slider slider = (Slider)sender;
    if(slider != null)
    this.sliderValueTextBlock.Text = slider.Value.ToString();

    So, for me it's not clear why I can't get those value from binding directly instead of using code-behing.

    I would appreciate any help.

    Thanks a lot !

    Saturday, November 23, 2013 10:45 PM

All replies

  • i tested it and it worked with me, no problem in binding, but i noticed that your text block has no width and height, that is why you cannot see it :D try the following code and tell me if it worked

            <Slider x:Name="slider" HorizontalAlignment="Left" Margin="521,300,0,0" VerticalAlignment="Top" Width="618"/>
            <TextBlock x:Name="sliderValueTextBlock" HorizontalAlignment="Left" Margin="839,403,0,0" Text="{Binding ElementName=slider, Path=Value}" VerticalAlignment="Top" Height="56" Width="233"/>

    • Proposed as answer by Ahmed-Fouad Sunday, November 24, 2013 5:03 AM
    Sunday, November 24, 2013 5:03 AM
  • Hi Ahmed,

    I see your irony :) but size of a TextBlock is not the case here - all TextBlocks have auto size for both Width and Height by default. So if you not mentioned otherwize they will be autosized acording to TextBlock's content (in our case - text).

    I meant that the Value of property is equal to 0 all the time - that behavior is reflected in TextBlock's Text property.

    I tried to create project from scratch and I see no problem - everything works fine now.

    But the major difficulty here is that I need to use Prism library in my project - they provide MvvmAppBase class as a base for App class in Metro app.

    When I tried to re-create my project I didn't use the Prism - just usual App class. And it works, I mean when navigated to MainPage Slider which I placed on that page ferlects changes when I drag the Thumb.

    But when I use Prism's MvvmAppBase - everything is broken again.

    D'you know is it really true that usage of Prism's MvvmAppBase class could lead to bad page initialization ? (Because I can't see more meaningful explanation here).


    Sunday, November 24, 2013 6:46 PM
  • Hi,

    Would you mind uploading your project to SkyDrive. I cannot reproduce the problem.

    Best Wishes!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Edited by Anne Jing Thursday, November 28, 2013 8:40 AM edit
    Thursday, November 28, 2013 8:39 AM