Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 更改主题仅应用于窗口背景色_Wpf_Visual Studio_Xaml - Fatal编程技术网

Wpf 更改主题仅应用于窗口背景色

Wpf 更改主题仅应用于窗口背景色,wpf,visual-studio,xaml,Wpf,Visual Studio,Xaml,这是我先前回答问题的延伸 我有一个组合框,它可以在ThemeBlue.xaml和ThemeRed.xaml之间更改主题 下面是示例项目文件: ThemeBlue.xaml: themerd.xaml: 蓝色主题 红色主题 问题: 选择红色时,仅更改窗口背景色,而不更改文本框或按钮 组合框主题选择 XAML <ComboBox x:Name="cboTheme" Style="{StaticResource ComboBoxCustom}" HorizontalAlignment

这是我先前回答问题的延伸


我有一个
组合框
,它可以在
ThemeBlue.xaml
ThemeRed.xaml
之间更改主题

下面是示例项目文件:

ThemeBlue.xaml:
themerd.xaml:

蓝色主题

红色主题

问题:
选择红色时,仅更改窗口背景色,而不更改
文本框
按钮


组合框主题选择 XAML

<ComboBox x:Name="cboTheme" Style="{StaticResource ComboBoxCustom}" HorizontalAlignment="Left" Margin="199,120,0,0" VerticalAlignment="Top" Width="75" SelectionChanged="themeSelect_SelectionChanged">
    <System:String>Blue</System:String>
    <System:String>Red</System:String>
    <System:String>Purple</System:String>
</ComboBox>
App.xaml 启动时通过
App.xaml
加载主题文件

<Application x:Class="MultiTheme.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MultiTheme"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ThemeBlue.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

main窗口中设置
Style
属性时,请使用
DynamicResource
标记扩展而不是
StaticResource

<Button x:Name="button" Style="{DynamicResource ButtonCustom}" Content="Button" HorizontalAlignment="Left" Margin="304,171,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="textBox" Style="{DynamicResource TextBoxCustom}" HorizontalAlignment="Left" Height="78" Margin="28,77,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="351"/>


我已将其应用到我的大型项目中,效果良好。谢谢你的帮助。
<TextBox x:Name="textBox1" Style="{StaticResource TextBoxCustom}" HorizontalAlignment="Left" Height="22" Margin="93,43,0,0" Width="464" />
<Button x:Name="button" Style="{DynamicResource ButtonCustom}" Content="Button" HorizontalAlignment="Left" Margin="304,171,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="textBox" Style="{DynamicResource TextBoxCustom}" HorizontalAlignment="Left" Height="78" Margin="28,77,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="351"/>