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

时间:2017-11-21 19:19:53

标签: c# wpf xaml datatemplate

我想将一个样式(在这种情况下为'ListViewItem')应用于DataTemplate中的控件。

样式(示例)代码:

<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(示例)代码:

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

将Background,BorderBrush和Foreground样式应用于DataTemplate中的<Label>有什么解决方案?

提前谢谢。

注意:我已经阅读了'Applying style to elements inside a DataTemplate'的问题和答案,但我想使用xaml(所以没有C#代码)。

1 个答案:

答案 0 :(得分:0)

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