小编And*_*yev的帖子

WPF - 在字典中定义的混合样式与父控件中定义的样式

我在资源字典中定义Button控件的自定义外观:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Style TargetType="Button" x:Key="BaseButtonStyle">
    <Setter Property="Background" Value="Blue"/>
  </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

然后我尝试在按钮所在的窗口中更改样式.

<Window.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Dictionary.xaml"/>
      <ResourceDictionary>
        <Style TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}">
          <Setter Property="Foreground" Value="Red"/>
        </Style>
      </ResourceDictionary>
      </ResourceDictionary.MergedDictionaries>   
    </ResourceDictionary>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

在WPF设计师中,我有我的期望.有红色文本的蓝色按钮.但在运行时,两种样式都不会应用,按钮具有默认颜色.我怎样才能解决这个问题?

.net c# wpf xaml

5
推荐指数
1
解决办法
2211
查看次数

标签 统计

.net ×1

c# ×1

wpf ×1

xaml ×1