簡體   English   中英

同時在兩個wpf datagrid中選擇一行

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

對於我正在從事的項目,我需要能夠例如在grid1中選擇第一行,並自動在grid2中也選擇第一行,以便在兩個網格中同時選擇一行。

已經嘗試了什么

  • 使用堆疊

我創建了一種方法來設置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);
        }
    }

我還查看了MSDN的文檔,其中涉及重點,但沒有成功。

GUI已經存在,因此無法重寫

這甚至有可能做到嗎?

問題尚不清楚。 如果始終選擇相同的行,則可以將所有數據放在一個數據網格中,除非需要特定的布局。 但是,讓我嘗試按原樣解決您的問題。 您可以將兩種選擇類型合並為一個類。 這兩個Datagrids可以彼此連接,而無需使用經典事件。 解決方案如下所示:

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#源代碼

    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM