【发布时间】:2011-11-11 02:02:02
【问题描述】:
我有以下场景:
<UserControl.Resources>
<Style x:Key="NormalFontStyle">
<Setter Property="Control.FontFamily" Value="{Binding MyFont}"></Setter>
</Style>
<Style x:Key="BigFontStyle">
<Setter Property="Control.FontFamily" Value="{Binding MyFont}"></Setter>
<Setter Property="Control.FontSize" Value="{Binding MyBigFontSize}"></Setter>
</Style>
</UserControl.Resources>
<Grid Style="{StaticResource NormalFontStyle}">
<!-- Grid Contents -->
</Grid>
Grid 的 DataContext 是包含 MyFont 和 MyBigFontSize 属性的 ViewModel。上面的代码工作正常,网格内的每个文本都应用了“NormalFontStyle”。
现在棘手的部分是:我想将“BigFontStyle”应用于网格内可能具有或不具有相同 DataContext 的控件,这意味着我不能使用这种方法。 也许将设置器的值绑定到静态属性是唯一的方法,(我刚刚找到了 3.5 的 this 解决方法,这是我的情况),但欢迎对此提出任何启示。
【问题讨论】:
标签: .net wpf xaml wpf-controls styles