[英]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.