[英]Bind a DataGrid to the selectedrow object of a second datagrid, WPF Caliburn.Micro
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. 我有一个绑定到BindableCollection的数据网格,该网格正常运行,我添加到BindableCollection的每个维修订单都在Datagrid中显示。
I have a second Datagrid on the same view for "WriteOffs", Each "RepairOrder" has a property of BindableCollection. 我在“ WriteOffs”的同一视图上有第二个Datagrid,每个“ RepairOrder”都有BindableCollection的属性。
What I am trying to do is bind the WriteOff DataGrid to the WriteOffs of the selected row. 我正在尝试将WriteOff DataGrid绑定到所选行的WriteOffs。 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”数据网格中选择一行时,都会在WriteOff数据网格中显示存储在writeoff属性中的注销。
What is the best way to handle this? 处理此问题的最佳方法是什么?
RepairOrder class: RepairOrder类:
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;
}
The XML for the Data grid showing the repair orders: 显示修复订单的数据网格的XML:
<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 for the Data grid for the write-offs: 用于注销的数据网格的XML:
<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>
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. 我当时想在用户选择一行时创建一个事件,但是如果将选定的行放入ViewModel方法中,我似乎找不到找到获取值的方法。
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... 好的,这就是我要做的方式...
Instead of SelectedIndex
on your ScheduleGrid DataGrid
you need to use SelectedItem
. 代替
SelectedIndex
您ScheduleGrid DataGrid
你需要使用SelectedItem
。 So your XAML
would look like this: 因此,您的
XAML
如下所示:
<DataGrid x:Name="ScheduleGrid" ItemsSource="{Binding RepairOrders}" SelectedItem="{Binding SelectedRepairOrder} ...."
Not to the ViewModel
Now you need to make the SelectedItem
property, or SelectedRepairOrder
. 不使用
ViewModel
现在,您需要创建SelectedItem
属性或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);
}
}
Second, since we have two DataGrids
we need also two Collections
. 其次,由于我们有两个
DataGrids
,所以我们还需要两个Collections
。 The ScheduleGrid
should have a Collection
who looks like this: ScheduleGrid
应该具有一个如下所示的Collection
:
private BindableCollection<RepairOrder> _repiarOrdersCollection;
public BindableCollection<RepairOrder> RepairOrders
{
get { return _repiarOrdersCollection; }
set
{
_repiarOrdersCollection = value;
}
}
And the WriteOffs DataGrid
collection should be like this: 而
WriteOffs DataGrid
集合应如下所示:
public BindableCollection<WriteOff> WriteOffs
{
get
{
return 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. 好的...现在发生了什么...正如您在
SelectedRepairOrder
属性中看到的那样,更改后,它将通知您的WriteOffs
集合已更改。 And since we are newer setting the value of its DataGrid
, we don't need any setter
. 而且由于我们是更新的设置其
DataGrid
的值,我们不需要任何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. 如果是,则需要扩展
GetWriteOffs()
方法,使其具有一些king, string, int...
参数,并在其中过滤数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.