WPF Bool DataTrigger - 更改字体颜色

时间:2013-06-22 16:32:58

标签: c# wpf datatrigger

我想根据用户选择的主题更改WPF页面上多个项目的字体颜色。 (有一个轻主题,一个黑暗的主题)

我正在使用数据模板来自定义列表框的内容,当主题为黑暗时,我想将所有文本块字体颜色更改为白色。

我在页面后面的课程我将'DarkTheme'设置为true。

我已成功设置了下面的触发器,但我无法弄清楚如何从页面类的值设置中执行此操作。

那么,如果DarkTheme = true,我如何将文本块的字体颜色设置为白色?

代码:

public partial class Media : UserControl
    {

        public bool DarkTheme { get; set; }

        readonly DatabaseAsset _dbAssets = new DatabaseAsset();

        public Media()
        {
            InitializeComponent();                        
            RefreshMediaList();
            DarkTheme = Global.Configuration.IsDarkModeAppliedAsTheme();

        }
}

XAML:

<UserControl.Resources>
        <db:MediaAsset x:Key="MediaAsset"/>        
        <DataTemplate x:Key="MediaAssetItemTemplate">
            <ListBoxItem Height="70" Name="ListBoxItem">
                <DockPanel Margin="0,0,0,0" Height="65">          
                    <DockPanel DockPanel.Dock="Left" Name="VideoImage2" Height="65" Width="102">
                        <Button Name="ListBoxItemSelect" Click="ButtonBase_OnClick" Tag="{Binding Path=Id}">
                            <Path Name="test" Width="38" Height="30.0833" Canvas.Left="19" Canvas.Top="22.1667" Stretch="Fill" Fill="#FF000000" Data="F1 M 19,34.8333L 22.1667,34.8333L 22.1667,42.75L 19,42.75L 19,34.8333 Z M 22.9583,34.0417L 49.4791,34.0417L 49.4791,38L 50.6667,38L 57,31.6667L 57,52.25L 50.6667,45.9167L 49.4791,45.9167L 49.4791,52.25L 22.9583,52.25L 22.9583,34.0417 Z M 29.2917,22.1667C 32.3522,22.1667 34.8333,24.6478 34.8333,27.7083C 34.8333,30.7689 32.3522,33.25 29.2917,33.25C 26.2311,33.25 23.75,30.7689 23.75,27.7083C 23.75,24.6478 26.2311,22.1667 29.2917,22.1667 Z M 41.9583,22.1667C 45.0189,22.1667 47.5,24.6478 47.5,27.7083C 47.5,30.7689 45.0189,33.25 41.9583,33.25C 38.8977,33.25 36.4167,30.7689 36.4167,27.7083C 36.4167,24.6478 38.8977,22.1667 41.9583,22.1667 Z "/>                          
                        </Button>
                    </DockPanel>
                    <DockPanel Dock="Left" Name="VideoData2" HorizontalAlignment="Stretch" Height="65">
                        <TextBlock DockPanel.Dock="Top" Text="{Binding Path=Title}" FontWeight="Bold" FontSize="18"></TextBlock>
                        <TextBlock DockPanel.Dock="Top" TextTrimming="CharacterEllipsis" Text="{Binding Path=Description}" TextWrapping="NoWrap"  FontSize="13" Margin="0,0,0,0"/>
                        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=VideoCatNane}" FontSize="12" FontStyle="Italic"/>
                            <Border Width="50"></Border>
                            <TextBlock Text="{Binding Path=MediaState}" FontSize="12"/>
                        </StackPanel>
                    </DockPanel>                 
                </DockPanel>
            </ListBoxItem> 
        </DataTemplate>      
    </UserControl.Resources>

1 个答案:

答案 0 :(得分:0)

这不是在WPF中进行主题化的正确方法。

但是如果你想要一个快速解决方案,请将InitializeComponent()语句移到构造函数的底部:

    public Media()
    {

        RefreshMediaList();
        DarkTheme = Global.Configuration.IsDarkModeAppliedAsTheme();
        InitializeComponent(); // <-- Here

    }