简体   繁体   English

将(xaml)样式应用于DataTemplate控件

[英]Apply (xaml) style to DataTemplate control

I would like to apply a style, in this case of a 'ListViewItem', to a control within a DataTemplate. 我想将样式(在这种情况下为“ ListViewItem”)应用于DataTemplate中的控件。

Style (sample) code: 样式(示例)代码:

<Style x:Key="ListViewItemStyle" TargetType="ListViewItem">       
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="True" />
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Background" Value="Blue" />
                <Setter Property="BorderBrush" Value="Blue" />
                <Setter Property="Foreground" Value="White"/>
            </MultiTrigger.Setters>
        </MultiTrigger>
    </Style.Triggers>
</Style>

DataTemplate (sample) code: DataTemplate(示例)代码:

<ListView.View>
  <GridView>
    <GridViewColumn>
        <GridViewColumnHeader Content="Text"/>
        <GridViewColumn.CellTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding MyImage}"/>
                    <Label Content="{Binding MyLabelText}"/>
                </StackPanel>
            </DataTemplate>
        </GridViewColumn.CellTemplate>
    </GridViewColumn>
  </GridView>
</ListView.View>

What is the solution to apply the Background, BorderBrush and Foreground style to the <Label> within the DataTemplate? 将Background,BorderBrush和Foreground样式应用于DataTemplate中的<Label>的解决方案是什么?

Thank you in advance. 先感谢您。

Note: I already read the question and answers of 'Applying style to elements inside a DataTemplate' , but I would like to use xaml (so without C# code). 注意:我已经阅读了“将样式应用于DataTemplate中的元素”的问题和答案,但是我想使用xaml(因此没有C#代码)。

The following XAML styles the ListViewItem (assuming Items is your collection): 以下XAML设置了ListViewItem的样式(假设Items是您的集合):

<ListView ItemsSource="{Binding Items}">
    <ListView.Resources>
        <Style TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Trigger.Setters>
                        <Setter Property="Background" Value="Blue" />
                        <Setter Property="BorderBrush" Value="Blue" />
                    </Trigger.Setters>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="Label">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsSelected}" Value="True">
                    <DataTrigger.Setters>
                        <Setter Property="Foreground" Value="White"></Setter>
                    </DataTrigger.Setters>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumnHeader Content="Text"/>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding MyImage}" Width="20"/>
                            <Label Content="{Binding MyLabelText}" />
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM