我想设置我项目的所有用户控件的背景属性。
我试过了
<style TargetType={x:Type UserControl}>
<setter property="Background" Value="Red" />
</style>
它编译但没有工作。
任何想法?谢谢!
我想设置我项目的所有用户控件的背景属性。
我试过了
<style TargetType={x:Type UserControl}>
<setter property="Background" Value="Red" />
</style>
它编译但没有工作。
任何想法?谢谢!
您只能将 aa 样式设置为特定类,因此这将起作用(创建一个 UserControl 对象,不是很有用):
<Window.Resources>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<UserControl Name="control" Content="content"></UserControl>
</Grid>
但这不是(创建一个派生自 UserControl 的类):
<Window.Resources>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<l:MyUserControl Name="control" Content="content"></l:MyUserControl>
</Grid>
您可以做的是使用 Style 属性显式设置样式:
<Window.Resources>
<Style TargetType="{x:Type UserControl}" x:Key="UCStyle">
<Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<l:MyUserControl Name="control" Content="content" Style="{StaticResource UCStyle}"></l:MyUserControl>
</Grid>
或者为每个派生类创建一个样式,可以使用 BasedOn 避免重复样式内容:
<Window.Resources>
<Style TargetType="{x:Type UserControl}" x:Key="UCStyle">
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="{x:Type l:MyUserControl}" BasedOn="{StaticResource UCStyle}" />
</Window.Resources>
<Grid>
<l:MyUserControl Name="control" Content="content"></l:MyUserControl>
</Grid>
我认为您缺少一些双引号:
尝试这个:
<Window.Resources>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<UserControl Name="control" Content="content"></UserControl>
</Grid>