如何为ItemContainerStyle创建ResourceDictionary?

时间:2019-03-03 16:15:52

标签: c# .net wpf mvvm resourcedictionary

所以我的XAML中有这个ListView,它变得越来越大,我想将完成的某些样式分离到ResourceDictionary中。

这就是现在的样子。

<ListView Grid.Row="1"
          x:Name="NotesListView"
          ItemsSource="{Binding NotesViewModel.Notes}">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="3"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" 
                    Value="Stretch"/>
        </Style>
    </ListView.ItemContainerStyle>

    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Width="100"
                  Height="100"
                  Background="{Binding Color}">
                <StackPanel Margin="10">
                    <TextBlock Text="{Binding Title}"/>
                    <TextBlock Text="{Binding Description}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我想将此部分放入ResourceDictionary

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <UniformGrid Columns="3"/>
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" 
                Value="Stretch"/>
    </Style>
</ListView.ItemContainerStyle>

但是我不是怎么做的。 据我所知

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="ListView">

    </Style>

</ResourceDictionary>

Style中,我无法添加ListView.ItemContainerStyle 那么我如何正确地将其分成ResourceDictionary?

1 个答案:

答案 0 :(得分:0)

资源字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ItemsPanelTemplate x:Key="lstViewItemsPanelTemplate">
        <UniformGrid Columns="3"/>
    </ItemsPanelTemplate>
    <Style TargetType="ListViewItem" x:Key="lstViewItemContainerStyle">
        <Setter Property="HorizontalContentAlignment" 
                Value="Stretch"/>
    </Style>
    <DataTemplate x:Key="lstViewItemTemplate">
        <Grid Width="100"
              Height="100"
              Background="{Binding Color}">
            <StackPanel Margin="10">
                <TextBlock Text="{Binding Title}"/>
                <TextBlock Text="{Binding Description}"/>
            </StackPanel>
        </Grid>
    </DataTemplate>
</ResourceDictionary>

列表视图

<ListView ItemsPanel="{StaticResource lstViewItemsPanelTemplate}"
          ItemContainerStyle="{StaticResource lstViewItemContainerStyle}"
          ItemTemplate="{StaticResource lstViewItemTemplate}"/>

您还可以在Style中定义全局ResourceDictionary.xaml,例如

<Style TargetType="ListView">
    <Setter Property="ItemsPanel" Value="{StaticResource lstViewItemsPanelTemplate}"/>
    <Setter Property="ItemContainerStyle" Value="{StaticResource lstViewItemContainerStyle}"/>
    <Setter Property="ItemTemplate" Value="{StaticResource lstViewItemTemplate}"/>
</Style>