C# 为什么控件没有';t选择为其控件定义的通用样式';s型
我正在尝试对表单中的所有边框应用以下样式:C# 为什么控件没有';t选择为其控件定义的通用样式';s型,c#,wpf,xaml,styles,wpf-style,C#,Wpf,Xaml,Styles,Wpf Style,我正在尝试对表单中的所有边框应用以下样式: <UserControl.Resources> <Style TargetType="{x:Type Border}"> <Setter Property="BorderBrush" Value="#5076A7" /> <Setter Property="BorderThickness" Value="1" /> <Setter Property=
<UserControl.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="#5076A7" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CornerRadius" Value="4" />
</Style>
<Style ... />
<UserControl.Resources>
这里缺少什么?您可以将BorderStyle添加到ControlTemplate.Resources中 以下是代码:
<ListView>
<ListViewItem Content="asdasd"/>
<ListViewItem Content="asdasd"/>
<ListViewItem Content="asdasd"/>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ControlTemplate.Resources>
<Style x:Key="ListViewItemBorderStyle" TargetType="{x:Type Border}">
<Setter Property="Background" Value="Red"/>
<Setter Property="BorderBrush" Value="#5076A7" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CornerRadius" Value="4" />
</Style>
</ControlTemplate.Resources>
<Border Height="100" Style="{StaticResource ListViewItemBorderStyle}">
<!--....-->
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</ListView.ItemContainerStyle>
</ListView>
我通过将边框样式分配给该样式的
解决了这个问题,其中的嵌套样式被描述为概念,如下所示:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border>
<Grid Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
...
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
<!--Nested Style-->
<Style.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="1" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="White" />
</Style>
</Style.Resources>
</Style>
</ListView.ItemContainerStyle>
...
我为您的问题编写了两个样式代码。您可以使用style.Resources或ControlTemplate.Resources。此外,您还找到了正确的解决方案
以下是代码:
(使用DynamicSource)
另一个代码(使用StaticResource)。它嵌入在ControlTemplate中
<ListView>
<ListViewItem Content="asdasd"/>
<ListViewItem Content="asdasd"/>
<ListViewItem Content="asdasd"/>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ControlTemplate.Resources>
<Style x:Key="ListViewItemBorderStyle" TargetType="{x:Type Border}">
<Setter Property="Background" Value="Red"/>
<Setter Property="BorderBrush" Value="#5076A7" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CornerRadius" Value="4" />
</Style>
</ControlTemplate.Resources>
<Border Height="100" Style="{DynamicResource ListViewItemBorderStyle}">
<!--....-->
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</ListView.ItemContainerStyle>
</ListView>
它被嵌入到一个ControlTemplate中,这就是为什么。您可以像使用Style=所说的那样显式声明它,也可以将它放入控件模板或指向模板的BasedOn声明中。@ChrisW。对于第二个建议,BasedOn
是否需要针对相同的类型?而且,一般情况下是否嵌入了一个问题,或者只有ControlTemplate
具有这种性质?我只是想进一步了解一下,把ControlTemplate想象成它自己的野兽,而忽略了当前DOM中它将/可能继承其样式的任何其他内容。但是,您仍然可以在视图中的某个位置指定该样式,或在资源字典中指定该样式,并在该ControlTemplate中的.Resources中执行类似操作,而不是反复重复整个模板。谢谢,但为什么不在
中指定呢?
<Window.Resources>
<Style x:Key="ListViewItemBorderStyle" TargetType="{x:Type Border}">
<Setter Property="Background" Value="Red"/>
<Setter Property="BorderBrush" Value="#5076A7" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CornerRadius" Value="4" />
</Style>
</Window.Resources>
<ListView>
<ListViewItem Content="asdasd"/>
<ListViewItem Content="asdasd"/>
<ListViewItem Content="asdasd"/>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border Height="100" Style="{DynamicResource ListViewItemBorderStyle}">
<!--....-->
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</ListView.ItemContainerStyle>
</ListView>
<ListView>
<ListViewItem Content="asdasd"/>
<ListViewItem Content="asdasd"/>
<ListViewItem Content="asdasd"/>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ControlTemplate.Resources>
<Style x:Key="ListViewItemBorderStyle" TargetType="{x:Type Border}">
<Setter Property="Background" Value="Red"/>
<Setter Property="BorderBrush" Value="#5076A7" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CornerRadius" Value="4" />
</Style>
</ControlTemplate.Resources>
<Border Height="100" Style="{DynamicResource ListViewItemBorderStyle}">
<!--....-->
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</ListView.ItemContainerStyle>
</ListView>