简体   繁体   English

同时在两个wpf datagrid中选择一行

[英]selecting a row in two wpf datagrid's at the same time

For i project that I'm working on i need to be able the for example selected the first row in grid1 and automatically have the first row also selected in grid2 so that a row is selected in both grids at the same time. 对于我正在从事的项目,我需要能够例如在grid1中选择第一行,并自动在grid2中也选择第一行,以便在两个网格中同时选择一行。

what is have already tried 已经尝试了什么

  • using a stackpanal 使用堆叠

i created a method for setting the selected item in the gids 我创建了一种方法来设置GIDS中的所选项目

        private void SyncDataGridItemSelection(int number)
        {
        //sync pricegrid with printertick grid
        if (number == 1)
        {
            int printertickselection = PrinterTicksGrid.SelectedIndex;
            //detachs change event
            PrinterPriceGrid.SelectionChanged -= (PrinterPriceGrid_SelectionChanged);
            //change printerpricegrid selection
            PrinterPriceGrid.SelectedIndex = printertickselection;
            //attach change event
            PrinterPriceGrid.SelectionChanged += (PrinterPriceGrid_SelectionChanged);
        }
        //sync printertick grid with price grid
       else if (number == 2)
        {
            int printerpriceselection = PrinterPriceGrid.SelectedIndex;
            //detachs change event
            PrinterTicksGrid.SelectionChanged -= (PrinterTicksGrid_SelectionChanged);
            //change PrinterTicksgrid selection
            PrinterTicksGrid.SelectedIndex = printerpriceselection;
            //attach change event
            PrinterTicksGrid.SelectionChanged += (PrinterTicksGrid_SelectionChanged);
        }
    }

i have also looked at documention from MSDN involving focus but to no success. 我还查看了MSDN的文档,其中涉及重点,但没有成功。

the GUI is already in place so rewriting is not an option GUI已经存在,因此无法重写

Is this even possible to do and if so how? 这甚至有可能做到吗?

The question is unclear. 问题尚不清楚。 If you always select the same rows, then you could put all data in one datagrid unless you need a specific layout. 如果始终选择相同的行,则可以将所有数据放在一个数据网格中,除非需要特定的布局。 But let me try to solve your question as it is. 但是,让我尝试按原样解决您的问题。 You can merge the two selection types into one class. 您可以将两种选择类型合并为一个类。 The two Datagrids can be connected to each other without using classical events. 这两个Datagrids可以彼此连接,而无需使用经典事件。 The solution would look like this: 解决方案如下所示:

XAML XAML

    <Window x:Class="WpfApplication2.MainWindow"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    Title="MainWindow" Height="229.584" Width="270.818">
        <Grid>
            <DataGrid AutoGenerateColumns="False" Name="DG1" HorizontalAlignment="Left" Height="190" Margin="10,0,0,0" VerticalAlignment="Top" Width="116" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Data A" Binding="{Binding AData.data}"/>
                </DataGrid.Columns>
            </DataGrid>
            <DataGrid AutoGenerateColumns="False" Name="DG2" HorizontalAlignment="Left" Height="190" Margin="131,0,0,0" VerticalAlignment="Top" Width="116" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Data B" Binding="{Binding BData.data}"/>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>

C# source code C#源代码

    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Documents;

    namespace WpfApplication2 {
        public class A {
            public int data { get; private set; }
            public A(int x) { data = x; }
        }

        public class B {
            public string data { get; private set; }
            public B(string x) { data = x; }
        }

        public class C {
            public A AData { get; set; }
            public B BData { get; set; }
        }

        public partial class MainWindow : Window {

            public MainWindow() {
                InitializeComponent();

                List<A> lIntegers = new List<A>() { new A(1), new A(2), new A(3), new A(4) };
                List<B> lStrings = new List<B>() { new B("a"), new B("b"), new B("c"), new B("d") };
                List<C> lParent = new List<C>();

                for (int i = 0; i < 4; i++) {
                    C c = new C();
                    lParent.Add(c);
                    c.AData = lIntegers[i];
                    c.BData = lStrings[i];
                }

                DG1.DataContext = lParent;
                DG2.DataContext = lParent;
            }
        }
    }

在此处输入图片说明

www.ohta.de www.ohta.de

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM