将DataGrid绑定到第二个数据网格WPF Caliburn.Micro的selectedrow对象 [英] Bind a DataGrid to the selectedrow object of a second datagrid, WPF Caliburn.Micro

查看:62
本文介绍了将DataGrid绑定到第二个数据网格WPF Caliburn.Micro的selectedrow对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到BindableCollection的数据网格,它工作正常,我添加到BindableCollection的每个维修订单都显示在Datagrid中。

I have a datagrid that is bound to a BindableCollection, this is working properly, every repair order I add to the BindableCollection shows in the Datagrid.

我有一个在 WriteOffs的同一视图上的第二个Datagrid中,每个 RepairOrder都有BindableCollection的属性。

I have a second Datagrid on the same view for "WriteOffs", Each "RepairOrder" has a property of BindableCollection.

我想要做的是将WriteOff DataGrid绑定到WriteOffs。所选行的因此,每次用户在 RepairOrder数据网格中选择一行时,都会在WriteOff数据网格中显示存储在writeoff属性中的注销。

What I am trying to do is bind the WriteOff DataGrid to the WriteOffs of the selected row. So every time the user selects a row in the "RepairOrder" datagrid the write offs stored in the writeoff property is shown in the WriteOff datagrid.

什么是最佳方法

RepairOrder类:

RepairOrder class:

public string ControlNumber { get; set; }
        public double Value { get; set; }
        public string Note { get; set; }
        public string Schedule { get; set; }
        public int Age { get; set; }
        public List<WriteOff> WriteOffs { get; set; }



        public RepairOrder(string CN, string SC, double VL)
        {
            ControlNumber = CN;
            Schedule = SC;
            Value = Math.Round(VL,2);
            Note = null;
            WriteOffs = new List<WriteOff>();
        }

        public RepairOrder()
        {

        }

        public void AddWriteOff(WriteOff WO)
        {
            WriteOffs.Add(WO);
        }

        public BindableCollection<WriteOff> GetWriteOffs()
        {
            BindableCollection<WriteOff> temp = new BindableCollection<WriteOff>();
            foreach (var item in WriteOffs)
            {
                temp.Add(item);
            }
            return temp;
        }

        public static RepairOrder FromCSV(string CSVLine, string Sched)
        {
            string[] values = CSVLine.Split(',');
            RepairOrder rep = new RepairOrder();
            rep.ControlNumber = values[2];
            rep.Value = Math.Round(double.Parse(values[5]),2);
            rep.Age = int.Parse(values[4]);
            rep.Schedule = Sched;
            return rep;
        }

数据网格的XML显示维修订单:

The XML for the Data grid showing the repair orders:

<Border BorderBrush="Black" BorderThickness="2" CornerRadius="5" Grid.Column="1" Grid.Row="1">
                <DataGrid  x:Name="ScheduleGrid" ItemsSource="{Binding RepairOrders}" CanUserSortColumns="True" AutoGenerateColumns="False" SelectedIndex="{Binding SelectedRepairOrder}" SelectionMode="Single">

                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Schedule" Binding="{Binding Schedule}" Width="75" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Control Number" Binding="{Binding ControlNumber}" Width="110" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Age" Binding="{Binding Age}" Width="50" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Value" Binding="{Binding Value, StringFormat=C}" Width="75" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Note" Binding="{Binding Note}" Width="*"/>
                    </DataGrid.Columns>
                </DataGrid>
            </Border>

用于销毁的数据网格的XML:

XML for the Data grid for the write-offs:

<Border Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" BorderBrush="Black" BorderThickness="2" CornerRadius="5" Margin="5,2,5,5">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="Write Off List" HorizontalAlignment="Center" FontSize="20"/>
                        <DataGrid ItemsSource="{Binding WriteOffs}" AutoGenerateColumns="False">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Account" Binding="{Binding Account}" Width="100" IsReadOnly="True"/>
                                <DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="200" IsReadOnly="True"/>
                                <DataGridTextColumn Header="Amount" Binding="{Binding WriteOffAmount}" Width="*" IsReadOnly="True"/>
                            </DataGrid.Columns>
                        </DataGrid>
                    </StackPanel>
                </Border>

我在想当用户选择一行时创建一个事件,但是我似乎无法如果将选定的行放入ViewModel方法中,则找到一种获取值的方法。

I was thinking of making an event when the user selects a row, but I can't seem to find a way to get the value if the selected row into the ViewModel method.

我似乎找不到清晰的教程或确切地介绍如何处理这种情况。

I can't seem to find a clear tutorial or post on exactly how to hand this situation.

实现最终目标的最简单方法是什么?

What is the easiest way to accomplish my final goals?

推荐答案

好的,这就是我要做的方式...

Okay this is the way I would do...

在您的<$ c $上代替 SelectedIndex c> ScheduleGrid DataGrid ,则需要使用 SelectedItem
所以您的 XAML 看起来像这样:

Instead of SelectedIndex on your ScheduleGrid DataGrid you need to use SelectedItem. So your XAML would look like this:

 <DataGrid  x:Name="ScheduleGrid" ItemsSource="{Binding RepairOrders}" SelectedItem="{Binding SelectedRepairOrder} ...."

不使用 ViewModel
现在,您需要制作 SelectedItem 属性,或 SelectedRepairOrder
该属性应如下所示:

Not to the ViewModel Now you need to make the SelectedItem property, or SelectedRepairOrder. The property should look like this:

 private RepairOrder _selectedRepairOrder;
 public RepairOrder SelectedRepairOrder
        {
            get { return _selectedRepairOrder; }
            set
            {
                if (_selectedRepairOrder == value) return;
                _selectedRepairOrder = value;
                NotifyOfPropertyChange(() => SelectedRepairOrder);
                NotifyOfPropertyChange(() => WriteOffsCollection);
            }
        }

第二,因为我们有两个 DataGrids ,我们还需要两个 Collections
ScheduleGrid 应该有一个如下所示的 Collection

Second, since we have two DataGrids we need also two Collections. The ScheduleGrid should have a Collection who looks like this:

   private BindableCollection<RepairOrder> _repiarOrdersCollection;
        public BindableCollection<RepairOrder> RepairOrders
        {
            get { return _repiarOrdersCollection; }
            set
            {
                _repiarOrdersCollection = value;
            }
        }

WriteOffs DataGrid 集合应该像这样:

        public BindableCollection<WriteOff> WriteOffs
        {
            get
            {
                return GetWriteOffs();
            }
        }

好吧...现在发生了什么...您可以在 SelectedRepairOrder 属性中看到,更改后,它将通知您的 WriteOffs 集合已更改。并且由于我们是较新的设置其 DataGrid 的值,因此不需要任何设置器
现在缺少一件事。既然您有两个收藏夹,我相信您想从一个收藏夹中选择一个商品来过滤另一个收藏夹中的商品吗?对?如果是,则需要扩展 GetWriteOffs()方法,使其具有某些国王,字符串,整数... 并在其中过滤数据。

Okay... now what happens... As you can see in your SelectedRepairOrder property, after it changes, it will notify your WriteOffs collection that it has changed. And since we are newer setting the value of its DataGrid, we don't need any setter. Now one thing is missing. Since you have two collections, I believe that you want after selecting item from one collection to filter the items on other collection? Right? If yes, the you need to extend your GetWriteOffs() method, to have parameters of some king, string, int... and inside it filter your data.

这篇关于将DataGrid绑定到第二个数据网格WPF Caliburn.Micro的selectedrow对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆