Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am new to the C# and trying the elements bindings. I created a slider bar and textbox to control the fontsize of textblock. The following snippet is not working,

C#
<TextBlock Name="lblText" Height="23" HorizontalAlignment="Left" Margin="20,20,0,0" Text="TextBlock" VerticalAlignment="Top" />
<Slider Name="sldValue" Value="{Binding ElementName=txbValue, Path=Text, Mode=TwoWay}"  Height="23" HorizontalAlignment="Left" Margin="20,60,0,0"  VerticalAlignment="Top" Width="200" Minimum="1" Maximum="40"/>
<TextBox Name="txbValue" Text="{Binding ElementName=lblText, Path=FontSize, Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="20,100,0,0" VerticalAlignment="Top" Width="120" />


However, if I move the binding of textbox to textblock, it works well,

C#
<TextBlock Name="lblText" FontSize="{Binding ElementName=txbValue, Path=Text, Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="20,20,0,0" Text="TextBlock" VerticalAlignment="Top" />
<Slider Name="sldValue" Value="{Binding ElementName=txbValue, Path=Text, Mode=TwoWay}"  Height="23" HorizontalAlignment="Left" Margin="20,60,0,0"  VerticalAlignment="Top" Width="200" Minimum="1" Maximum="40"/>
<TextBox Name="txbValue"  Height="23" HorizontalAlignment="Left" Margin="20,100,0,0" VerticalAlignment="Top" Width="120" />
Posted

1 solution

Try this:

XML
<StackPanel>
            <TextBlock x:Name="Text" Text="Example" FontSize="{Binding ElementName=TextBox, Path=Text}"/>
            <Slider x:Name="Slider" Value="{Binding ElementName=TextBox, Path=Text}" Minimum="10" Maximum="50"/>
            <TextBox x:Name="TextBox" Text="{Binding ElementName=Slider, Path=Value}"/>
        </StackPanel>


FontSize of TextBlock changes when typing a number in the TextBox and also changes when moving the slider.
Hope this is the solution to your problem.
 
Share this answer
 
Comments
Rence Y 21-Aug-13 21:37pm    
However, as there is 3 elements, 2 tow-ways bindings should be enough for binding them all together.

So, with refer to the xaml of the original question, the binding from the textbox to the textblock has no effects.

I believe there's more considerations of the binding declarations to put.

Thanks Marvin for kindly helps....Could anyone can give some glue to explain the behaviors ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900