Changing the styles at runtime in WPF

前端 未结 3 839
走了就别回头了
走了就别回头了 2020-12-03 20:58

I am trying to allow the user to customize the elements in a WPF application. What I am trying to achieve is, if I have a list box which specifies all the form elements (Tex

相关标签:
3条回答
  • 2020-12-03 21:27

    it worked for me like a charm:

    Xaml:

    <TreeView x:Name="TreePeople">
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="True" />
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView> 
    

    c#:

    bool Expanded = false; 
    // The event subscription method (for a button click)
    private void ButtonExpand__Click(object sender, RoutedEventArgs e)
    {
        Expanded = !Expanded;
        Style Style = new Style
        {
            TargetType = typeof(TreeViewItem)
        };
    
        Style.Setters.Add(new Setter(TreeViewItem.IsExpandedProperty, Expanded));
        TreePeople.ItemContainerStyle = Style;
    }
    
    0 讨论(0)
  • 2020-12-03 21:28

    Have you tried using Resource Dictionaries

    Resource Dictionary

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="TextColor" Color="#FF121212"/>
    </ResourceDictionary>
    

    XAML for the control

    <TextBox Text="TextBox" Foreground="{DynamicResource TextColor}" />
    

    Code to change styles at runtime

         var rd = new ResourceDictionary();
         rd.Add("TextColor", "#FFFFFF");
         Application.Current.Resources.MergedDictionaries.Add(rd);
    

    This will merge your new styles with the existing ones, and the change will be automatically reflected on all the controls linked with those styles.

    0 讨论(0)
  • 2020-12-03 21:35

    You have to make sure that the styles are in the file App.xaml:

    <Application x:Class="ChangeStyleHelp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    
        <Application.Resources>
            <Style x:Key="MyStyle" TargetType="{x:Type Label}">
                <Setter Property="Background" Value="Green" />
            </Style>
        </Application.Resources>
    </Application>
    

    Code behind:

    private void ChangeStyle_Click(object sender, RoutedEventArgs e)
    {
        Style style = new Style 
        { 
            TargetType = typeof(Label) 
        };
    
        style.Setters.Add(new Setter(Label.BackgroundProperty, Brushes.Aquamarine));
    
        Application.Current.Resources["MyStyle"] = style;
    }   
    

    If the Style is in the resource of Window (Window.Resources), then you need to write this, or the name of the Window:

    private void ChangeStyle_Click(object sender, RoutedEventArgs e)
    {
        Style style = new Style 
        { 
            TargetType = typeof(Label) 
        };
    
        style.Setters.Add(new Setter(Label.BackgroundProperty, Brushes.Aquamarine));
    
        this.Resources["MyStyle"] = style;
    }   
    

    As well, you can change the Style this way: take an existing style and use of the element. Example:

    <Application x:Class="ChangeStyleHelp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    
        <Application.Resources>
            <Style x:Key="AnotherWayStyle" TargetType="{x:Type Label}">
                <Setter Property="Background" Value="Lavender" />
                <Setter Property="Foreground" Value="OrangeRed" />
            </Style>
        </Application.Resources>
    </Application>  
    

    Code behind:

    private void AnotherWay_Click(object sender, RoutedEventArgs e)
    {
        label1.Style = (Style)Application.Current.Resources["AnotherWayStyle"];
    }   
    
    0 讨论(0)
提交回复
热议问题