0

我有这个 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>

而且我希望能够将它放在资源目录文件中,以便我可以将它重用于其他类,但我不知道如何将 xaml 转换为包含我需要的样式标签..

4

1 回答 1

0

更新:由于 GroupStyle 属性公开了一个集合,因此仅使用 XAML 进行管理非常复杂。但是,如果您接受一次只应用一种样式,这里有一个解决方法,以便重用以下代码。

基本上创建一个派生自 DependencyObject 的类,定义一个附加的 DP,它将根据 ListView 属性管理 GroupStyle 的插入/删除。

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>

干杯

于 2013-06-23T10:40:10.077 回答