简体   繁体   English

如何为ItemContainerStyle创建ResourceDictionary?

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

So I have this ListView in my XAML which is getting pretty big, I would like to seperate some of the styling done into a ResourceDictionary. 所以我的XAML中有这个ListView,它变得越来越大,我想将完成的某些样式分离到ResourceDictionary中。

This is what it looks like right now. 这就是现在的样子。

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

I would like to put this part into a ResourceDictionary 我想把这部分放到ResourceDictionary

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

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

But I am not so how how to. 但是我不是那么怎么做。 This is as far as I got 据我所知

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

    <Style TargetType="ListView">

    </Style>

</ResourceDictionary>

And within the Style I can't add ListView.ItemContainerStyle So how di I properly seperate it into a ResourceDictionary? Style我无法添加ListView.ItemContainerStyle那么,如何正确地将其分隔为ResourceDictionary?

Resource Dictionary 资源字典

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

List View 列表显示

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

or 要么

You can also define a global Style in ResourceDictionary.xaml like, 您还可以在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.

相关问题 如何覆盖在ResourceDictionary中指定的DataGridColumnHeader? - How do I override the DataGridColumnHeader specified in my ResourceDictionary? 如何在ResourceDictionary中设置可用于背景属性的主要颜色 - How do I set a primary color that I can use for my background property in ResourceDictionary 当我的文本框在resourcedictionary中时,如何保存文本输入? - How can I save text input when my textbox is in resourcedictionary? 如何从另一个程序集中将URI设置为ResourceDictionary中的对象? - How do I set the URI to an object in a ResourceDictionary from another assembly? 如何将资源添加到具有ResourceDictionary的类中 - How do I add resources to a class which has ResourceDictionary 如何使用应用程序ResourceDictionary进行RuntimeType映射 - How to use the Application ResourceDictionary to do RuntimeType mapping 在ResourceDictionary中创建对Locator的引用 - Create a reference to Locator in a ResourceDictionary 如何在ItemContainerStyle内添加效果 - How to add effect inside ItemContainerStyle WinRT-创建具有特定ItemContainerStyle的自定义ListView控件 - WinRT - Create a custom ListView control with specific ItemContainerStyle 如何创建测试以查看我的AI是否完美? - How do I create a test to see if my A.I. is perfect?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM