简体   繁体   中英

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.

what is have already tried

  • using a stackpanal

i created a method for setting the selected item in the 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.

the GUI is already in place so rewriting is not an option

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. The solution would look like this:

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

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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