Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 排序和交替列表视图_C#_Wpf_Xaml_Listview - Fatal编程技术网

C# 排序和交替列表视图

C# 排序和交替列表视图,c#,wpf,xaml,listview,C#,Wpf,Xaml,Listview,我不知道如何为我的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/

我不知道如何为我的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>

我应该如何实现这一点?

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

  • 您既不设置数据上下文,也不显式指定绑定源
  • 甚至没有任何东西可以绑定到
    items
    属性
  • 您声明但不使用
    CollectionViewSource
    ,因此它在任何情况下都无效
  • 代码示例中也没有显示您如何尝试使用
    alternationandex
    ,因此不可能说出您做错了什么

    尽管如此,基本思想还是很简单的。既然您找到了关于
    alternationandex
    的示例,我认为您理所当然地也找到了问题的
    CollectionViewSource
    部分,只是没有理解这些示例。我在下面列出了您的代码版本,它解决了我上面提到的所有问题,并展示了使用
    alternationandex
    在输出中每行产生格式差异的一种可能方法:

    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>
    
    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();
        }
    }
    
    
    
    C#:

    <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>
    
    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();
        }
    }
    
    公共部分类主窗口:窗口
    {
    公共列表项{get;private set;}
    公共主窗口()
    {
    项目=新列表();
    添加(新用户(){Name=“John Doe”,Result=42});
    添加(新用户(){Name=“Jane Doe”,Result=39});
    添加(新用户(){Name=“sammydoe”,Result=13});
    初始化组件();
    }
    }
    
    User
    类与以前一样,所以我没有费心把它包括在这里

    您将在上面的示例中注意到以下更改:

  • 我给了窗口一个名称,以便将其用作绑定源
  • 我将局部变量
    items
    移出构造函数,并使其成为类中的属性。请注意,由于我没有生成更改通知,因此在
    InitializeComponent()
    方法之前设置属性并填充列表内容也很重要
  • 我没有绑定到原始列表,而是使用
    CollectionViewSource
    作为绑定源进行绑定,从而确保
    ListView
    使用
    CollectionViewSource
    中的视图

  • 请仔细地将以上内容与原始代码进行比较,以便您了解每个特定问题是如何解决的。如果您有任何问题,请随时提问。一定要清楚、准确地说明您在理解方面遇到的具体问题。

    请比“它不起作用”更具体一些。您发布的代码似乎无效:没有数据上下文,也没有任何要绑定到
    items
    属性的内容,而且您似乎没有使用
    CollectionViewSource
    。当您运行代码时,它到底做了什么,这与您想要的有什么不同?至于
    alternationandex
    问题,你应该发布一个新的问题,其中显示你是如何尝试使用它的,并精确描述代码的作用以及它与你想要的有什么不同。好吧,我了解得更多,但不知怎的,它仍然没有在窗口中显示结果。我复制你的代码,以确保我没有犯任何错误,而且名单上仍然没有一个人。好的!发现简单问题,谢谢!:)我花了很多时间和精力,而你做得太快了!:)