0

我想根据用户选择的主题更改 WPF 页面上许多项目的字体颜色。(有浅色主题和深色主题)

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

我正在将“DarkTheme”设置为 true 的页面后面的课程。

我已经成功地在下面设置了触发器,但是我无法从 pages 类上设置的值中弄清楚如何做到这一点。

那么,如果 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>
4

1 回答 1

0

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

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

    public Media()
    {

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

    }
于 2013-06-22T17:02:15.927 回答