0

I have a application that already uses a style template to defining font family, font size, foregrounds colors and so on.

One funcionnality of that app is enable users to picker a particular color and then apply to all texts, including textblocks, listviews, buttons and etc.

I've already read this link (Find all controls in WPF Window by type), where I could find all objects' type of FrameworkElement and then apply the color to each element according with his type. However, I am not sure that is the best approach.

Community
  • 1
  • 1
FelipeFV
  • 5
  • 1
  • 3
    For all your elements that must be able to change color, bind the color property to one central Color, that you can change. – Peter Bruins Mar 14 '17 at 18:03

1 Answers1

0

As you said, there are lots of way of doing this. For starter, you can have a style template and then use StaticResource to get the style for it. For example:in your view mainwindow.xaml

<Window.Resources>
    <Style TargetType="{x:Type Control}" x:Key="baseStyle">
        <Setter Property="FontSize" Value="100" />
    </Style>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}"></Style>
    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource baseStyle}"></Style>
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource baseStyle}"></Style>
    <Style TargetType="{x:Type ListView}" BasedOn="{StaticResource baseStyle}"></Style>
    <!-- ComboBox, RadioButton, CheckBox, etc... -->
</Window.Resources>

OR Another way is: View: mainwindow.xaml

<Window>
    <Window.resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="FontSize"
                Value="14" />
    </Style>
    </Window.resource>

    <Grid>
       <TextBox Text="blah"/>
    </Grid>
</Window>

OR

you can do it from mainwindow constructor

Style = (Style)FindResource(typeof(Window));

and this, you will put this in the app.xaml

<Style TargetType="{x:Type Window}">
    <Setter Property="FontSize"
            Value="14" />
</Style>
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291