c# - WPF Datagrid 排序索引问题

标签 c# wpf sorting datagrid indexing

我似乎无法理解这一点。

我已经创建了示例代码来演示我的问题,希望有人能指导我找到答案...

问题是数据网格排序后,标记的 ID 和名称不再与选定的数据网格项目匹配。

我会感谢你的帮助...

谢谢

杰夫

<Window x:Class="dgSortTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="dgSortTest" Height="253" Width="403" IsEnabled="True">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="212" HorizontalAlignment="Left" Margin="12,2,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" SelectionChanged="dataGrid1_SelectionChanged" RowHeaderWidth="0" AreRowDetailsFrozen="False" CanUserAddRows="True" CanUserDeleteRows="True" IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        </DataGrid.Columns>
        </DataGrid>
        <Label Content="Index: " Name="lblIndex" Height="28" HorizontalAlignment="Left" Margin="228,12,0,0" VerticalAlignment="Top" Width="92" />
        <Label Content="ID:" Name="lblID" Height="28" HorizontalAlignment="Left" Margin="228,46,0,0" VerticalAlignment="Top" Width="141" IsEnabled="True" />
        <Label Content="Name: " Name="lblName" Height="28" HorizontalAlignment="Left" Margin="228,80,0,0" VerticalAlignment="Top" Width="141" />
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace dgSortTest
{
    public partial class MainWindow : Window
    {
       List<Person> people = new List<Person>();
       public MainWindow()
        {
            InitializeComponent();
            people.Add(new Person() { ID = 0, Name = "Jeff" });
            people.Add(new Person() { ID = 1, Name = "Tom" });
            people.Add(new Person() { ID = 2, Name = "Andy" });
            people.Add(new Person() { ID = 3, Name = "Ken" });
            people.Add(new Person() { ID = 4, Name = "Zack" });
            people.Add(new Person() { ID = 5, Name = "Emily" });
            people.Add(new Person() { ID = 6, Name = "Courtney" });
            people.Add(new Person() { ID = 7, Name = "Adam" });
            people.Add(new Person() { ID = 8, Name = "Brenda" });
            people.Add(new Person() { ID = 9, Name = "Bill" });
            people.Add(new Person() { ID = 10, Name = "Joan" });
            dataGrid1.ItemsSource = from Person in people select Person;
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = dataGrid1.SelectedIndex;
            lblIndex.Content = "Index: " + index.ToString();
            lblID.Content = "ID: " + people[index].ID;
            lblName.Content = "Name: " + people[index].Name;
        } 
    }

    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}

最佳答案

很好地描述了您的问题和简短的示例,为此 +1。

简短的回答,为了不改变你当前的大部分实现,你可以这样做。

private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Person selectedItem = dataGrid1.SelectedItem as Person;
    int index = dataGrid1.SelectedIndex;
    lblIndex.Content = "Index: " + index.ToString();
    lblID.Content = "ID: " + selectedItem.ID;
    lblName.Content = "Name: " + selectedItem.Name;
}

但是,我建议您改为直接绑定(bind)到 SelectedItem。那么您将不需要 EventHandler 背后的代码。

<StackPanel Orientation="Vertical" Margin="228,12,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="92" >
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Index: "/>
        <TextBlock Text="{Binding ElementName=dataGrid1, Path=SelectedIndex}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="ID: "/>
        <TextBlock Text="{Binding ElementName=dataGrid1, Path=SelectedItem.ID}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Name: "/>
        <TextBlock Text="{Binding ElementName=dataGrid1, Path=SelectedItem.Name}"/>
    </StackPanel>
</StackPanel>

关于c# - WPF Datagrid 排序索引问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4109434/

相关文章:

c# - 更新 WPF MVVM 中的 UI 元素

ruby - 按 Ruby 中哈希的值降序排序

arrays - 对先递增后递减的数组进行排序

c# - 如何将10个类似的事件方法转换为一个方法来处理上述10个事件?

c# - 如何从mysql数据库显示图像到datagridview

c# - C#中奇怪的 float 运算操作

java - 使用 JdbcTemplate 对 DB 记录进行排序

c# - .NET Web DLL 是否有类似 Application.ProductVersion 之类的东西?

.net - WPF、Silverlight 或 XNA : Choosing a platform for game development

.net - TPL WPF MVVM 如何使其工作?