Określenie wskazywanego wiersza/kolumny datagrid WPF

0

Witam, jak najprościej określić wskazywaną kolumnę/wiersz w datagrid WPF.

0

w xaml czy codebehind ?

0

Po indeksie ? Czy po prostu nie rozumiem pytania. :P

0

Miałem na myśli naciśnięty przez użytkownika wiersz, jego numer(w przypadku gdy SelectionUnit = FullRow). A jeśli jesteście w stanie powiedzieć to także numery naciśniętej komórki w przypadku, gdy SelectionUnit = Cell.

0

Nie ważne czy SelectionUnit="FullRow" czy "Cell" działa dobrze.

<StackPanel>
     <DataGrid Name="datagrid" ItemsSource="{Binding}" SelectionUnit="FullRow"/>
     <Button Content="Test" Click="Button_Click" IsCancel="True"/>
</StackPanel>
    public partial class MainWindow : Window
    {
        ObservableCollection<Person> people = new ObservableCollection<Person>
        {
            new Person{Name = "Tom", Age = 10},
            new Person{Name = "Ken", Age = 20},
            new Person{Name = "Jen", Age = 30},
        };
        public MainWindow()
        {
            InitializeComponent();
            DataContext = people;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (datagrid.SelectedCells.Count > 0)
            {
                DataGridCell cell = DataGridHelper.GetCell(datagrid.SelectedCells[0]);
                this.Title = DataGridHelper.GetRowIndex(cell).ToString();
            }
        }
    }
    public class Person
    {
        public string Name { set; get; }
        public int Age { set; get; }
    }
    public static class DataGridHelper
    {
        public static DataGridCell GetCell(DataGridCellInfo dataGridCellInfo)
        {
            if (!dataGridCellInfo.IsValid)
            {
                return null;
            }

            var cellContent = dataGridCellInfo.Column.GetCellContent(dataGridCellInfo.Item);
            if (cellContent != null)
            {
                return (DataGridCell)cellContent.Parent;
            }
            else
            {
                return null;
            }
        }
        public static int GetRowIndex(DataGridCell dataGridCell)
        {
            // Use reflection to get DataGridCell.RowDataItem property value.
            PropertyInfo rowDataItemProperty = dataGridCell.GetType().GetProperty("RowDataItem", BindingFlags.Instance | BindingFlags.NonPublic);

            DataGrid dataGrid = GetDataGridFromChild(dataGridCell);

            return dataGrid.Items.IndexOf(rowDataItemProperty.GetValue(dataGridCell, null));
        }
        public static DataGrid GetDataGridFromChild(DependencyObject dataGridPart)
        {
            if (VisualTreeHelper.GetParent(dataGridPart) == null)
            {
                throw new NullReferenceException("Control is null.");
            }
            if (VisualTreeHelper.GetParent(dataGridPart) is DataGrid)
            {
                return (DataGrid)VisualTreeHelper.GetParent(dataGridPart);
            }
            else
            {
                return GetDataGridFromChild(VisualTreeHelper.GetParent(dataGridPart));
            }
        }
    }

Źródło: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0cdf2968-00e8-4ea5-aea0-ffd0e8230110

0

dzięki, nieco to skomplikowane bardziej.

1 użytkowników online, w tym zalogowanych: 0, gości: 1