【发布时间】:2015-09-02 16:05:40
【问题描述】:
在我的 wpf 应用程序中,我想为我的所有文本框设置样式。结果我的 App.xaml 看起来像:
<Application x:Class="IM.WindowsApp.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 TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Yellow"/>
</Style>
</Application.Resources>
</Application>
在 MainWindow.xaml 我有:
<TextBox Text="Some Text" />
当我运行它时,文本显示为黄色,就像我预期的那样。
这是我的问题
现在我想为该文本框添加一些 additional 样式。结果我修改了我的代码看起来像
<TextBox Text="Some Text">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Opacity" Value=".8" />
</Style>
</TextBox.Style>
</TextBox>
当我运行时,我的文本框前景不再是黄色:(。我不想替换样式。
解决方案是给原始样式一个资源键。然后我可以放置BasedOn={StaticRecource MyResourceKey}。这不是一个解决方案,因为我必须将Style="{StaticResource MyResourceKey}" 添加到我的应用程序的所有文本框中。我想避免这样做。
【问题讨论】:
标签: wpf xaml styles resourcedictionary