我有一个WPF用户控件,其中包含多个标签(动态创建)。我想通过我的用户控件设置这些标签的样式(每个标签都应具有相同的样式)。
因此,简单地说,设置用户控件的样式应将该样式应用于所有标签。
可以在UserControl资源中声明Label的样式,并将Setters值绑定到UserControl属性,例如:
<UserControl x:Class="WpfApplication3.UserControl1"
x:Name="Uc1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<Style TargetType="Label">
<Setter Property="Foreground"
Value="{Binding Foreground, ElementName=Uc1, Mode=OneWay}"/>
</Style>
</UserControl.Resources>
<Grid>
<Label Content="123"/>
</Grid>
</UserControl>
这样,更改UserControl前景将影响内部的所有标签(如果它们不覆盖前景设置)
样式可以使用类型名称(Label
)作为键,并且默认情况下将应用样式。或者它可以具有其他一些键,并且应该显式分配给动态创建的Labels
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句