我有这个XAML:
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
</ItemsPanelTemplate>
</GroupStyle.Panel>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupItem">
<WrapPanel Orientation="Horizontal">
<ContentPresenter Margin="0,0,0,0" VerticalAlignment="Center" />
<ItemsPresenter Margin="0,0,0,0" VerticalAlignment="Center"/>
</WrapPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="BorderThickness" Value="0,5,10,5"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Transparent" />
</Style.Resources>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<controls:AlignableWrapPanel HorizontalContentAlignment="Center" Width="90" AllowDrop="True">
<Image Height="50" HorizontalAlignment="Center" Width="50" Source="{Binding Path=Icon}"/>
<Label Width="90" HorizontalContentAlignment="Center" Content="{Binding Path=Name}"/>
</controls:AlignableWrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
我希望能够将它放在resourcedirectory文件中,以便我可以将其重用于其他类,但我不知道如何转换xaml以包含我需要的样式标签..
答案 0 :(得分:0)
更新:由于GroupStyle属性公开了一个集合,因此仅使用XAML进行管理非常复杂。但是,如果您接受一次只应用一种样式,则可以使用以下代码来重复使用以下代码。
基本上创建一个派生自DependencyObject的类,定义一个附加的DP,它将管理GroupStyle对ListView属性的插入/删除。
public class ListViewHelpers : DependencyObject
{
public static readonly DependencyProperty GroupStyleProperty = DependencyProperty.RegisterAttached(
"GroupStyle",
typeof(GroupStyle),
typeof(ListViewHelpers),
new PropertyMetadata(
null,
GroupStyleChanged
));
public GroupStyle GetGroupStyle(ListView target)
{
return (GroupStyle)GetValue(GroupStyleProperty);
}
public void SetGroupStyle(ListView target, GroupStyle value)
{
SetValue(GroupStyleProperty, value);
}
private static void GroupStyleChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var lv = (ListView)sender;
GroupStyle gs;
gs = args.OldValue as GroupStyle;
if (gs != null)
{
lv.GroupStyle.Remove(gs);
}
gs = args.NewValue as GroupStyle;
if (gs != null)
{
lv.GroupStyle.Add(gs);
}
}
}
之后,您应该声明所需的GroupStyle:
<GroupStyle x:Key="myGroupStyle">
...
</GroupStyle>
然后,在ListView的XAML声明中,您可以编写如下
<ListView myNS:ListViewHelpers.GroupStyle="{StaticResource myGroupStyle}">
...
</ListView>
以下是原始答案,这是错误的,但保持为痕迹。
两种方式。
第一种方式是声明GroupStyle本身
<GroupStyle x:Key="myGroupStyle">
...
</GroupStyle>
然后将其称为:
<ListView GroupStyle="{StaticResource myGroupStyle}">
...
</ListView>
第二种方式是整个ListView的样式,这是最好的解决方案,恕我直言:
<Style x:Key="myListView" TargetType="{x:Type ListView}">
<Setter Property="GroupStyle">
<Setter.Value>
<GroupStyle>
...
</GroupStyle>
</Setter.Value>
</Setter>
</Style>
干杯