簡體   English   中英

如何為ItemContainerStyle創建ResourceDictionary?

[英]How do I create a ResourceDictionary for my ItemContainerStyle?

所以我的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?

資源字典

<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}"/>

要么

您還可以在ResourceDictionary.xaml定義全局Style ,例如,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM