WPF C# 修改ListView组头-数据绑定

发布于 2025-01-16 19:22:47 字数 6994 浏览 2 评论 0原文

每个人。

我想创建一个 ListView 来列出 Orders 类中的项目。该列表应按进程分组。

当我将其创建为简单的 ListView 时,它工作正常。 我按照本指南创建了它

现在,我不仅希望将项目显示为列表项目,还希望将它们显示为图块。 瓷砖显示给我看。但是,在标题中不再根据哪个流程步骤进行分组。我也没有收到错误消息。

屏幕截图

谁能告诉我如何正确执行此操作?

非常感谢。

MyClass“ORDER”

namespace VersuchBindungDataTemplate
    {
    public class Order
        {
        public string Process { get; set; }

        public string Auftrag { get; set; }

        public string Material { get; set; }

        public string Type { get; set; }
        }
    }

**My Code behind:**


    public partial class MainWindow : Window
    {
        ObservableCollection<Order> items = new();

        public MainWindow()
        {

            InitializeComponent();

            items.Add(new Order() { Process = "Schweißen", Auftrag = "5121511", Material = "71231521", Type = "SD5" });
            items.Add(new Order() { Process = "Elmo", Auftrag = "5121513", Material = "71231523", Type = "SD7" });
            items.Add(new Order() { Process = "Öl füllen", Auftrag = "5121514", Material = "71231526", Type = "SD5" });
            items.Add(new Order() { Process = "Schweißen", Auftrag = "5121511", Material = "71231521", Type = "SD5" });
            items.Add(new Order() { Process = "Schweißen", Auftrag = "5121513", Material = "71231523", Type = "SD7" });
            items.Add(new Order() { Process = "Öl füllen", Auftrag = "5121514", Material = "71231526", Type = "SD5" });
            items.Add(new Order() { Process = "Schweißen", Auftrag = "5121511", Material = "71231521", Type = "SD5" });
            items.Add(new Order() { Process = "Elmo", Auftrag = "5121513", Material = "71231523", Type = "SD7" });
            items.Add(new Order() { Process = "Öl füllen", Auftrag = "5121514", Material = "71231526", Type = "SD5" });
            lvOrders.ItemsSource = items;

            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvOrders.ItemsSource);
            PropertyGroupDescription groupDescription = new PropertyGroupDescription("Process");
            view.GroupDescriptions.Add(groupDescription);
            view.Filter = UserFilter;
        }

        private bool UserFilter(object item)
        {
            if (string.IsNullOrEmpty(txtFilter.Text))
            {
                return true;
            }
            else
            {
                return (item as Order).Auftrag.Contains(txtFilter.Text, StringComparison.OrdinalIgnoreCase);
            }
        }

        private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            CollectionViewSource.GetDefaultView(lvOrders.ItemsSource).Refresh();
        }
    }

    
}

我的 XAML:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="150*"/>
        </Grid.RowDefinitions>
        <TextBox  Grid.Row="0" Name="txtFilter" TextChanged="txtFilter_TextChanged" Height="20" Margin="10" />

        <ListView Margin="10" Name="lvOrders" Grid.Row="1" ItemTemplate="{StaticResource ExplorerView}" SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListView.Resources>
                <c:Order x:Key="Orders" />
            </ListView.Resources>
            <ListView.DataContext>
                <Binding Source="{StaticResource Orders}"/>
            </ListView.DataContext>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Margin="10" />
                </ItemsPanelTemplate>


            </ListView.ItemsPanel>


            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Expander IsExpanded="True">
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal" Width="800">
                                                    <TextBlock Text="{Binding Process}" FontWeight="Bold" Foreground="Black" FontSize="24"/>
                                                    <TextBlock Text="Aufträge: " FontSize="22" Foreground="Silver" FontStyle="Italic"/>
                                                    <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic"/>
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Process}"/>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>

    </Grid>

我的 AppResources:

       <Application.Resources>
        <ResourceDictionary>
            <!-- Tile-style layout-->
            <DataTemplate x:Key="ExplorerView">
                <Border BorderThickness="3" BorderBrush="LightGray">
                    <StackPanel Background="LightGray" Width="150" Height="150">
                        <TextBlock Text="{Binding Process}" FontSize="9" HorizontalAlignment="Left"  />
                        <TextBlock Text="{Binding Auftrag}" FontSize="18" HorizontalAlignment="Left"  />
                        <TextBlock Text="{Binding Material}" FontSize="9" HorizontalAlignment="Left"  />
                        <TextBlock Text="{Binding Type}" FontSize="9" HorizontalAlignment="Left"  />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ResourceDictionary>
    </Application.Resources>

everyone.

I want to create a ListView that lists items from the Orders class. The list should be grouped by Process.

When I create it as a simple ListView it works fine. I created it following this guide.

Now I don't just want the items to be displayed as list items, but to display them as tiles.
The tiles are shown to me. However, in the header no longer the process step according to which is grouped. I don't get an error message either.

Screenshot

Can anyone show me how to do it right?

Many thanks in advance.

MyClass "ORDER":

namespace VersuchBindungDataTemplate
    {
    public class Order
        {
        public string Process { get; set; }

        public string Auftrag { get; set; }

        public string Material { get; set; }

        public string Type { get; set; }
        }
    }

**My Code behind:**


    public partial class MainWindow : Window
    {
        ObservableCollection<Order> items = new();

        public MainWindow()
        {

            InitializeComponent();

            items.Add(new Order() { Process = "Schweißen", Auftrag = "5121511", Material = "71231521", Type = "SD5" });
            items.Add(new Order() { Process = "Elmo", Auftrag = "5121513", Material = "71231523", Type = "SD7" });
            items.Add(new Order() { Process = "Öl füllen", Auftrag = "5121514", Material = "71231526", Type = "SD5" });
            items.Add(new Order() { Process = "Schweißen", Auftrag = "5121511", Material = "71231521", Type = "SD5" });
            items.Add(new Order() { Process = "Schweißen", Auftrag = "5121513", Material = "71231523", Type = "SD7" });
            items.Add(new Order() { Process = "Öl füllen", Auftrag = "5121514", Material = "71231526", Type = "SD5" });
            items.Add(new Order() { Process = "Schweißen", Auftrag = "5121511", Material = "71231521", Type = "SD5" });
            items.Add(new Order() { Process = "Elmo", Auftrag = "5121513", Material = "71231523", Type = "SD7" });
            items.Add(new Order() { Process = "Öl füllen", Auftrag = "5121514", Material = "71231526", Type = "SD5" });
            lvOrders.ItemsSource = items;

            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvOrders.ItemsSource);
            PropertyGroupDescription groupDescription = new PropertyGroupDescription("Process");
            view.GroupDescriptions.Add(groupDescription);
            view.Filter = UserFilter;
        }

        private bool UserFilter(object item)
        {
            if (string.IsNullOrEmpty(txtFilter.Text))
            {
                return true;
            }
            else
            {
                return (item as Order).Auftrag.Contains(txtFilter.Text, StringComparison.OrdinalIgnoreCase);
            }
        }

        private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            CollectionViewSource.GetDefaultView(lvOrders.ItemsSource).Refresh();
        }
    }

    
}

My XAML:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="150*"/>
        </Grid.RowDefinitions>
        <TextBox  Grid.Row="0" Name="txtFilter" TextChanged="txtFilter_TextChanged" Height="20" Margin="10" />

        <ListView Margin="10" Name="lvOrders" Grid.Row="1" ItemTemplate="{StaticResource ExplorerView}" SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListView.Resources>
                <c:Order x:Key="Orders" />
            </ListView.Resources>
            <ListView.DataContext>
                <Binding Source="{StaticResource Orders}"/>
            </ListView.DataContext>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Margin="10" />
                </ItemsPanelTemplate>


            </ListView.ItemsPanel>


            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Expander IsExpanded="True">
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal" Width="800">
                                                    <TextBlock Text="{Binding Process}" FontWeight="Bold" Foreground="Black" FontSize="24"/>
                                                    <TextBlock Text="Aufträge: " FontSize="22" Foreground="Silver" FontStyle="Italic"/>
                                                    <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic"/>
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Process}"/>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>

    </Grid>

My AppResources:

       <Application.Resources>
        <ResourceDictionary>
            <!-- Tile-style layout-->
            <DataTemplate x:Key="ExplorerView">
                <Border BorderThickness="3" BorderBrush="LightGray">
                    <StackPanel Background="LightGray" Width="150" Height="150">
                        <TextBlock Text="{Binding Process}" FontSize="9" HorizontalAlignment="Left"  />
                        <TextBlock Text="{Binding Auftrag}" FontSize="18" HorizontalAlignment="Left"  />
                        <TextBlock Text="{Binding Material}" FontSize="9" HorizontalAlignment="Left"  />
                        <TextBlock Text="{Binding Type}" FontSize="9" HorizontalAlignment="Left"  />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ResourceDictionary>
    </Application.Resources>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下壹個目標 2025-01-23 19:22:47

您的 GroupStyle 不了解 Orders 类中的属性,因此它不了解 Process 是什么。看看 ItemCount,它是从哪里来的?不是来自 Orders 类。

幸运的是,您的 GroupStyle 知道它是按什么分组的,并且它有一个 Name,这是您在代码隐藏中将 Process 属性绑定到的内容。

因此,这应该是一个简单的修复,只需将 GroupStyle 内的 {Binding Process} 替换为 {Binding Name} 即可。

<StackPanel Orientation="Horizontal" Width="800">
    <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Black" FontSize="24"/>
    <TextBlock Text="Aufträge: " FontSize="22" Foreground="Silver" FontStyle="Italic"/>
    <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic"/>
</StackPanel>

您的输出现在将如下所示:

在此处输入图像描述

Your GroupStyle has no knowledge of the properties in your Orders class, so it doesn't understand what Process is. Take a look at ItemCount, where did that come from? Not from the Orders class.

Luckily, your GroupStyle knows what it is grouped by, and it has a Name, which is what you bound the Process property to in your code-behind.

So it should be an easy fix where you just replace your {Binding Process} with {Binding Name} inside of your GroupStyle.

<StackPanel Orientation="Horizontal" Width="800">
    <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Black" FontSize="24"/>
    <TextBlock Text="Aufträge: " FontSize="22" Foreground="Silver" FontStyle="Italic"/>
    <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic"/>
</StackPanel>

Your output will now look like this:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文