C# 没有绑定的样式触发器?
我使用foreach语句以编程方式用C# 没有绑定的样式触发器?,c#,wpf,xaml,C#,Wpf,Xaml,我使用foreach语句以编程方式用字符串填充listview,而不使用.itemssource,然后我创建了这个样式触发器(看起来是正确的) 然后我运行项目并单击列表视图项。。。背景是蓝色的 我只是想知道样式触发器是否需要使用数据绑定。。。或者如果我的扳机错了。。或者如果有人有任何想法 触发器工作正常,但问题是ListViewItem的控件模板实际上没有使用Background属性的值作为控件的背景色 相反,它使用SystemColors.HighlightBrush属性的值,默认情况下该
字符串填充listview,而不使用.itemssource,然后我创建了这个样式触发器(看起来是正确的)
然后我运行项目并单击列表视图项。。。背景是蓝色的
我只是想知道样式触发器是否需要使用数据绑定。。。或者如果我的扳机错了。。或者如果有人有任何想法 触发器工作正常,但问题是ListViewItem的控件模板实际上没有使用Background属性的值作为控件的背景色
相反,它使用SystemColors.HighlightBrush属性的值,默认情况下该属性为蓝色(因此它看起来总是像Windows中的选择)
该属性有一个与之关联的资源键,因此您可以使用相同的键定义一个新笔刷,然后ListView将使用该笔刷。现在也可以摆脱触发器了
<ListView.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green" />
</ListView.Resources>
您的问题与绑定无关,触发器也正常,只是无效
选定ListViewItem的背景色由ListViewItem的控件模板中的VisualStateManager设置,如中所示:
<ListView.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green" />
</ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="Border" Background="Transparent" ...>
<VisualStateManager.VisualStateGroups>
...
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames
Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource SelectedBackgroundColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
...
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green" />
</Style.Resources>
...
</Style>