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