列表框,的DataTemplate和触发器 [英] ListBox , DataTemplate and Triggers
问题描述
我有一个DataTemplate的ListBoxItem的,我想创造一个TRIGER,所以当用户点击一个项目的背景将改变,并在标签
i got a DataTemplate for a listboxitem and i want to create a triger , so when a user click an item the background will change and also the label
我的code:
<Window.Resources>
<Style x:Key="RoundedItem" TargetType="ListBoxItem">
<EventSetter Event="MouseDoubleClick" Handler="listViewItem_MouseDoubleClick" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="ItemBorder" CornerRadius="10" BorderBrush="Black" BorderThickness="1" Margin="1" Background="Transparent">
<Label Name="ItemLabel" Foreground="Red" >
<ContentPresenter />
</Label>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="ItemBorder" Property="Background" Value="DeepSkyBlue" />
<Setter TargetName="ItemLabel" Property="Foreground" Value="Orange" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="TitleTemplate" DataType="models:Title" >
<StackPanel>
<Image Source="{Binding ThumbFilePath}" Width="50" HorizontalAlignment="Center"/>
<Label Content="{Binding Name}" HorizontalAlignment="Center" />
<TextBlock Text="{Binding Description}" HorizontalAlignment="Center" TextWrapping="Wrap" Padding="5,5,5,5"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
发生了什么是文本块改变自己的颜色,而不是标签。
What happend is that the TextBlock change his color and not the label..
有谁知道为什么吗? 谢谢你。
anyone know why ? Thanks.
推荐答案
在的TextBlock
继承其父母在可视化树的前景定义。该标签
,在另一方面,定义其默认样式前景。
The TextBlock
inherits the Foreground definition from its parents in the visual tree. The Label
, on the other hand, defines the Foreground in its default style.
您的做法是非WPF样 - 你不应该包装在一个标签上的
控制。内容presenter
Your approach is "non-WPF-like" - you shouldn't wrap the ContentPresenter
in a Label
control.
正确的方法取决于你是否希望在该项目的所有文字来改变它的前景,或者仅仅是标签?
The right approach depends on whether you want all text in the item to change its Foreground, or just the label?
[在这两种情况下,没有明显的好处,以数据模板中使用标签
- 所以我会假设标签改为的TextBlock
] 的
[In both cases, there's no apparent benefit to using a Label
in the data template - so I'll assume that the label is changed to TextBlock
.]
如果回答上面的问题是,所有的文本应改为:在控件模板
的 ListBoxItem的
在触发IsSelected,从seccond二传手删除的TargetName =ItemLabel
因此最终的制定者是:
If the answer to the above question is that all text should be changed: in the ControlTemplate
of the ListBoxItem
, in the trigger for IsSelected, from the seccond setter remove TargetName="ItemLabel"
so the final setter is:
<Setter Property="Foreground" Value="Orange" />
这将改变该项目的前景,这将影响到数据模板均的TextBlock
S的前景。
This will change the foreground of the item that will affect the foreground of both TextBlock
s in the data template.
如果你想影响的TextBlocks的只有一个:
If you want to affect just one of the TextBlocks:
1. remove the setter for the foreground from the control template
2. add a trigger to your data template:
<DataTemplate>
<StackPanel>
<Image .../>
<TextBlock x:Name="Text01" ..../>
<TextBlock x:Name="Text02" ..../>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True">
<Setter TargetName="Text01" Property="Foreground" Value="Orange"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
附注:如果您有无使用标签
控制你的数据模板,结合其前景属性的列表框项目的前景,像这样:
Side note: if you have to use Label
control in your data template, bind its Foreground property to the Foreground of the list box item, like so:
<Label Foreground="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"....../>
如果这没有帮助,这意味着你的列表框项目继承其前景,所以使用:
If this doesn't help, it means that your list box item inherits its foreground, so use:
<Label Foreground="{Binding TextElement.Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"....../>
这篇关于列表框,的DataTemplate和触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!