Basically I have the following structure:
<Window ...
xmlns:my="http://schemas.company.com/WPF/Controls"
>
<Window.Resources>
<Style x:Key="MyStyle1" TargetType={x:Type TextBlock}>
...
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<my:MyUserControl1 />
<my:MyUserControl1 />
<my:MyUserControl2 />
<my:MyUserControl2 />
</Grid>
</Window>
<UserControl ...
>
<TextBlock Style={ ?? What Goes Here ??} />
</UserControl>
If you want the Style to be applied to all TextBlock
s, including the ones in MyUserControl
, just leave the x:Key out and it will be applied implictly
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green"/>
</Style>
If you want it to be set explicitly you can use DynamicResource
in the UserControl
s
<Window.Resources>
<Style x:Key="MyStyle1" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green"/>
</Style>
</Window.Resources>
<StackPanel>
<my:UserControl1 />
<my:UserControl1 />
<my:UserControl1 />
<my:UserControl1 />
</StackPanel>
<UserControl ...>
<TextBlock Style="{DynamicResource MyStyle1}" Text="TextBlock"/>
</UserControl>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With