WPF C# 修改ListView组头-数据绑定
每个人。
我想创建一个 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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 GroupStyle 不了解
Orders
类中的属性,因此它不了解Process
是什么。看看ItemCount
,它是从哪里来的?不是来自Orders
类。幸运的是,您的 GroupStyle 知道它是按什么分组的,并且它有一个
Name
,这是您在代码隐藏中将Process
属性绑定到的内容。因此,这应该是一个简单的修复,只需将 GroupStyle 内的
{Binding Process}
替换为{Binding Name}
即可。您的输出现在将如下所示:
Your GroupStyle has no knowledge of the properties in your
Orders
class, so it doesn't understand whatProcess
is. Take a look atItemCount
, where did that come from? Not from theOrders
class.Luckily, your GroupStyle knows what it is grouped by, and it has a
Name
, which is what you bound theProcess
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.Your output will now look like this: