ListBox,DataTemplate和Triggers

时间:2011-11-05 16:08:31

标签: c# .net wpf xaml

我有一个listboxitem的DataTemplate,我想创建一个triger,所以当用户点击一个项目时,背景会改变,而且标签

我的代码:

<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>

                                                                                                             

发生的事情是TextBlock改变了他的颜色而不是标签..

谁知道为什么? 感谢。

2 个答案:

答案 0 :(得分:4)

TextBlock在可视树中从其父项继承Foreground定义。另一方面,Label以其默认样式定义Foreground。

您的方法是“非类似WPF” - 您不应将ContentPresenter包裹在Label控件中。

正确的方法取决于您是否希望项目中的所有文字更改其前景,或仅仅是标签?

[在这两种情况下,在数据模板中使用Label没有明显的好处 - 所以我假设标签已更改为TextBlock。]

如果上述问题的答案是所有文字都应该更改:在ControlTemplate的{​​{1}}中,在IsSelected的触发器中,从第二个setter中删除ListBoxItem所以最终的制定者是:

TargetName="ItemLabel"

这将更改将影响数据模板中<Setter Property="Foreground" Value="Orange" /> 的前景的项目的前景。

如果您只想影响其中一个TextBlocks:

TextBlock

附注:如果您 在数据模板中使用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> 控件,请将其Foreground属性绑定到列表框项的前景,如下所示:

Label

如果这没有帮助,则表示您的列表框项继承其前景,因此请使用:

<Label Foreground="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"....../>

答案 1 :(得分:1)

我想要解决这个问题,我遇到了类似的问题,我将ListBox.ItemTemplate添加到我的ListBox中,然后样式不再适用于文本了。

我正在尝试显示一个语言列表(CultureInfo)供用户选择,但是我希望显示本机名称,而不是英文名称。出于某种原因,并非所有语言都在其CultureInfo中将其本机名称大写,并且NativeName是其名称的唯一实例,因此我需要将一个函数应用于CultureInfo.NativeName以自行大写这些名称。为了实现这一点,我在里面添加了一个带有数据模板的ItemTemplate,我在其上应用了转换器。

<ListBox IsSynchronizedWithCurrentItem="True" VerticalAlignment="Center" MinHeight="200" x:Name="cbLanguages" 
    ItemsSource="{Binding Path=SupportedCultures, Mode=OneWay, Source={StaticResource CultureResourcesDS}}"
    FontSize="24" HorizontalAlignment="Stretch" Width="300" Margin="10"
    Style="{DynamicResource ListBoxTemplate}" ItemContainerStyle="{DynamicResource ListBoxItemStyle}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding Converter={StaticResource NativeNameConverter}}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

经过一段时间的搜索,我在这里遇到了XAMeLi的答案,并且将我放在DataTemplate中的Label更改为TextBox,并且我创建的ListBoxItemStyle再次工作。

基本上,标签和文本框具有可被利用的不同特征,或者在这种情况下可能导致恼人的问题。以下是一些很好的解释,其中包含一些不同的例子:http://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/

相关问题