Answered by:
Binding dynamic Style to UserControl

Question
-
I try to change the Textsize based on a slider on the mainpage.
<Window.Resources> <Style TargetType="TextBlock"> <Setter Property="TextBlock.FontSize" Value="{Binding ElementName=sliderFontsize, Path=Value}/> </Style>
So on the mainpage its displayed ok. The usercontrol embedded on the mainpage somehow did not get the changed fontsize at all. It complains that it could not find the bound property :
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=sliderFontsize'. BindingExpression:Path=Value; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'FontSize' (type 'Double')
Binding is still a bit magic to me :)
Sunday, June 20, 2010 6:40 AM
Answers
-
The sliderFontSize control which is present in Main Page will not be visible to a textBlock inside your UserControl. You will have to bind your textblock to a Property visible across the application. i.e. sliderFontSize to a CustomObject(Application Level) and that CustomObject you can bind to the application level resource.Sunday, June 20, 2010 7:53 AM
-
Hi Shalan007,
Since the DataBinding can fine the element named sliderFontSize in the UserControl.
You could create a property and bind it to the TextBox.Text and the Style for the TextBlock. The property looks like a bridge connect the TextBox.Text and the Style:
<Window.Resources> <Style TargetType="TextBlock"> <Setter Property="TextBlock.FontSize" Value="{Binding FontSize}"/> </Style> </Window.Resources> <StackPanel> <TextBlock Text="aaaa"/> <l:UserControl1/> <TextBox Text="{Binding fontSize, UpdateSourceTrigger=PropertyChanged}"/> </StackPanel>
Code:
public partial class Window2 : Window { public int fontSize { get; set; } public Window2() { InitializeComponent(); this.DataContext = this; } }
Hope this helps.
Sincerely,
Bob Bao
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Jie Bao Monday, June 28, 2010 1:39 AM
Wednesday, June 23, 2010 6:31 AM
All replies
-
Try this:
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="400"> <Window.Resources> <Style TargetType="TextBlock"> <Setter Property="TextBlock.FontSize" Value="{Binding ElementName=sliderFontSize,Path=Text }"/> </Style> </Window.Resources> <StackPanel> <TextBlock Height="21" Width="100" Text="My Text"/> <TextBox Height="21" Width="100" Name="sliderFontSize" /> </StackPanel> </Window>
sliderFontSize Control should be visible to the TextBlock control.
Sunday, June 20, 2010 7:44 AM -
The sliderFontSize control which is present in Main Page will not be visible to a textBlock inside your UserControl. You will have to bind your textblock to a Property visible across the application. i.e. sliderFontSize to a CustomObject(Application Level) and that CustomObject you can bind to the application level resource.Sunday, June 20, 2010 7:53 AM
-
Hi Shalan007,
Since the DataBinding can fine the element named sliderFontSize in the UserControl.
You could create a property and bind it to the TextBox.Text and the Style for the TextBlock. The property looks like a bridge connect the TextBox.Text and the Style:
<Window.Resources> <Style TargetType="TextBlock"> <Setter Property="TextBlock.FontSize" Value="{Binding FontSize}"/> </Style> </Window.Resources> <StackPanel> <TextBlock Text="aaaa"/> <l:UserControl1/> <TextBox Text="{Binding fontSize, UpdateSourceTrigger=PropertyChanged}"/> </StackPanel>
Code:
public partial class Window2 : Window { public int fontSize { get; set; } public Window2() { InitializeComponent(); this.DataContext = this; } }
Hope this helps.
Sincerely,
Bob Bao
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Jie Bao Monday, June 28, 2010 1:39 AM
Wednesday, June 23, 2010 6:31 AM -
Thx. I'll try that one out.
Currently I am using code behind to update teh FontSize properties of my usercontrols manually...
------------------------- I Love Windows Phone http://modernappdesign.blogspot.com/Wednesday, June 23, 2010 7:31 AM