gpt4 book ai didi

c# - 排序和交替 ListView

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:21 24 4
gpt4 key购买 nike

我不知道如何为我的 XAML 实现功能

<Window x:Class="WpfApplicationLAB.HighScore"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplicationLAB"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
mc:Ignorable="d"
Title="HighScore" Height="300" Width="300">
<Window.Resources>
<CollectionViewSource x:Key="SortedItems" Source="{Binding items}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Result"/>
<scm:SortDescription PropertyName="Name"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>

<Grid>
<ListView Margin="10" Name="lvDataBinding" ItemsSource ="{Binding items}" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Height" Value="40" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Result}" FontWeight="Bold" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

</Grid>
</Window>

和 C#

   public partial class HighScore : Window
{
public HighScore()
{
InitializeComponent();
List<User> items = new List<User>();
items.Add(new User() { Name = "John Doe", Result = 42 });
items.Add(new User() { Name = "Jane Doe", Result = 39 });
items.Add(new User() { Name = "Sammy Doe", Result = 13 });
}
}
public class User
{
public string Name { get; set; }

public int Result { get; set; }

public override string ToString()
{
return this.Name + " " + this.Result;
}
}
}

尝试实现 CollectionView 排序但它不起作用,我应该更改什么?

第二件事是我想交替使用背景颜色和字体颜色。我还尝试从这个例子中实现交替功能:Alternate background color in Listview XAML .但我不明白 Property="ItemsControl.AlternationIndex"Value="0"

<DataTemplate DataType="system:String">
<!-- put your data template here -->
</DataTemplate>

我应该如何实现?

最佳答案

从您的问题中并不清楚您具体遇到了什么问题。但是,您发布的代码在很多方面都是错误的:

  1. 您既不设置数据上下文也不明确指定绑定(bind)源。
  2. 甚至没有要绑定(bind)的items 属性。
  3. 您声明但不使用 CollectionViewSource,因此它在任何情况下都无效。

代码示例中也没有显示您如何尝试使用 AlternationIndex,因此无法说出您做错了什么。

综上所述,基本思想很简单。由于您找到了有关 AlternationIndex 的示例,因此我认为您也为问题的 CollectionViewSource 部分找到了示例,只是未能理解这些示例。我在下面包含了您的代码版本,它解决了我上面提到的所有问题,并展示了一种使用 AlternationIndex 在输出中每行产生格式差异的可能方法:

XAML:

<Window x:Class="TestSO37246528SortAndAlternationIndex.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Title="MainWindow" Height="350" Width="525"
Name="mainWindow">

<Window.Resources>
<CollectionViewSource x:Key="SortedItems" Source="{Binding items, ElementName=mainWindow}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Result"/>
<scm:SortDescription PropertyName="Name"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>

<Grid>
<ListView Margin="10" Name="lvDataBinding"
ItemsSource ="{Binding Source={StaticResource SortedItems}}"
AlternationCount="2">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightBlue"/>
</Trigger>
</Style.Triggers>
<Setter Property="Height" Value="40" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Result}" FontWeight="Bold" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>

C#:

public partial class MainWindow : Window
{
public List<User> items { get; private set; }

public MainWindow()
{
items = new List<User>();
items.Add(new User() { Name = "John Doe", Result = 42 });
items.Add(new User() { Name = "Jane Doe", Result = 39 });
items.Add(new User() { Name = "Sammy Doe", Result = 13 });
InitializeComponent();
}
}

User 类和以前一样,所以我懒得在这里包含它。

您会在上面注意到以下更改:

  1. 我给窗口起了一个名字,这样我就可以将它用作绑定(bind)源。
  2. 我将局部变量 items 移出了构造函数,并使其成为类中的一个属性。请注意,由于我没有采取任何措施来生成更改通知,因此在 InitializeComponent() 方法之前设置属性和填充列表内容也很重要。
  3. 我没有绑定(bind)到原始列表,而是使用 CollectionViewSource 作为绑定(bind)源进行绑定(bind),从而确保 ListView 使用来自 CollectionViewSource< 的 View

请将以上内容与您的原始代码仔细比较,以便您了解每个具体问题是如何解决的。如果您有任何问题,请随时提问。请务必以清晰、准确的方式说明具体您在理解方面遇到的困难。

关于c# - 排序和交替 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37246528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com