具有不同 ItemsSource 的 WPF MVVM DataGrid RowDetails
WPF MVVM DataGrid RowDetails with different ItemsSource
我将三个 DataGrids
设置为单独的 UserControls
,每个 ItemsSource
绑定到我的 ViewModel
中的不同 DependencyProperty
。
当在第一个 DataGrid
中选择一行时,其他两个填充与所选行相关的信息。虽然这工作正常,但我希望第二个和第三个 DataGrid
显示在第一个 DataGrid
的 RowDetailsTemplate
中。
我遇到的问题是父 DataGrid
的 ItemsSource
覆盖了其他两个的 ItemsSource
,因此它们没有填充。我已经尝试过针对许多其他类似问题发布的解决方案,但 none 已经解决了我的问题。我的代码在下面,我希望我只是错过了一些明显的东西,但我们将不胜感激!
主数据网格
<DataGrid x:Name="PostDataGrid"
ItemsSource="{Binding WSEDriverList}"
SelectedItem="{Binding SelectedWSEDriverPOST}"
Style="{DynamicResource MainDataGridStyle}"
Margin="0,0,0,0"
Grid.Row="1" >
<DataGrid.Columns>
<DataGridTextColumn Header="HesId" Binding="{Binding HESID, Mode=OneWay}" Width="50"/>
<DataGridTextColumn Header="WseId" Binding="{Binding WSEID, Mode=OneWay}" Width="50"/>
<DataGridTextColumn Header="API" Binding="{Binding API, Mode=OneWay}" Width="198" />
<DataGridTextColumn Header="Request Date" Binding="{Binding Path=REQUEST_DATE, Mode=OneWay, StringFormat=dd/MM/yyyy HH:mm:ss}" Width="125"/>
<DataGridTextColumn Header="Result Date" Binding="{Binding Path=RESULT_DATE, Mode=OneWay, StringFormat=dd/MM/yyyy HH:mm:ss}" Width="125"/>
<DataGridTextColumn Header="Return Code" Binding="{Binding RETURN_CODE, Mode=OneWay, StringFormat=0}" Width="80" />
<DataGridTextColumn Header="Status" Binding="{Binding STATUS, Mode=OneWay, StringFormat=0}" Width="70" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate DataType="{x:Type viewmodel:WSEAuditViewModel}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Expander Header="NOTIFICATION" Grid.Row="0">
<ItemsControl ItemsSource="{Binding WSEDriverGETResult}">
<usercontrol:NOTIFICATIONUserControl/>
</ItemsControl>
</Expander>
<Expander Header="GET" Grid.Row="1">
<ItemsControl ItemsSource="{Binding WSEDriverGETResult}">
<usercontrol:GETUserControl/>
</ItemsControl>
</Expander>
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
数据网格 2(通知用户控件)
<DataGrid x:Name="NotificationDG"
ItemsSource="{Binding NotificationResult}"
数据网格 3(获取用户控件)
<DataGrid x:Name="GetDG"
ItemsSource="{Binding WSEDriverGETResult}"
编辑
这是我的 DependencyProperties
绑定到 DataGrid
的代码。
//----POST result list from WSE_DRIVER-----
public List<WSE_DRIVER> WSEDriverList
{
get { return (List<WSE_DRIVER>)GetValue(WSEDriverListProperty); }
set { SetValue(WSEDriverListProperty, value); }
}
public static readonly DependencyProperty WSEDriverListProperty =
DependencyProperty.Register("WSEDriverList",
typeof(List<WSE_DRIVER>),
typeof(WSEAuditViewModel),
new PropertyMetadata(WSEDriverListChanged_Callback));
private static void WSEDriverListChanged_Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
WSEAuditViewModel wse_win = (WSEAuditViewModel)d;
if (wse_win.WSEDriverList.Count > 0)
{
wse_win.SelectedWSEDriverPOST = wse_win.WSEDriverList.First();
}
}
//----Selected POST WSE_Driver----
public WSE_DRIVER SelectedWSEDriverPOST
{
get { return (WSE_DRIVER)GetValue(SelectedWSEDriverPOSTProperty); }
set { SetValue(SelectedWSEDriverPOSTProperty, value); }
}
public static readonly DependencyProperty SelectedWSEDriverPOSTProperty =
DependencyProperty.Register("SelectedWSEDriverPOST",
typeof(WSE_DRIVER),
typeof(WSEAuditViewModel),
new PropertyMetadata(SelectedWSEDriverPOSTChanged_Callback));
private static void SelectedWSEDriverPOSTChanged_Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Gets list and assigns to WSEDriverGETResult and NotificationResult
}
//----GET result from WSE_Driver----
public List<WSE_DRIVER> WSEDriverGETResult
{
get { return (List<WSE_DRIVER>)GetValue(WSEDriverGETResultProperty); }
set { SetValue(WSEDriverGETResultProperty, value); }
}
public static readonly DependencyProperty WSEDriverGETResultProperty =
DependencyProperty.Register("WSEDriverGETResult",
typeof(List<WSE_DRIVER>),
typeof(WSEAuditViewModel),
new PropertyMetadata(WSEDriverGETResultChanged_Callback));
private static void WSEDriverGETResultChanged_Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
//----Notification Info----
public List<SPEC_NOTIFICATION> NotificationResult
{
get { return (List<SPEC_NOTIFICATION>)GetValue(NotificationResultProperty); }
set { SetValue(NotificationResultProperty, value); }
}
public static readonly DependencyProperty NotificationResultProperty =
DependencyProperty.Register("NotificationResult",
typeof(List<SPEC_NOTIFICATION>),
typeof(WSEAuditViewModel),
new UIPropertyMetadata(NotificationResultChanged_Callback));
private static void NotificationResultChanged_Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
由于 NotificationResult
和 WSEDriverGETResult
属性与 WSEDriverList
属性 属于相同的 class,您应该能够使用 {RelativeSource}
绑定到父 DataGrid
:
的 DataContext
<DataGrid x:Name="NotificationDG" ItemsSource="{Binding DataContext.NotificationResult,
RelativeSource={RelativeSource AncestorType=DataGrid}}"></DataGrid>
<DataGrid x:Name="GetDG" ItemsSource="{Binding DataContext.WSEDriverGETResult,
RelativeSource={RelativeSource AncestorType=DataGrid}}"></DataGrid>
我将三个 DataGrids
设置为单独的 UserControls
,每个 ItemsSource
绑定到我的 ViewModel
中的不同 DependencyProperty
。
当在第一个 DataGrid
中选择一行时,其他两个填充与所选行相关的信息。虽然这工作正常,但我希望第二个和第三个 DataGrid
显示在第一个 DataGrid
的 RowDetailsTemplate
中。
我遇到的问题是父 DataGrid
的 ItemsSource
覆盖了其他两个的 ItemsSource
,因此它们没有填充。我已经尝试过针对许多其他类似问题发布的解决方案,但 none 已经解决了我的问题。我的代码在下面,我希望我只是错过了一些明显的东西,但我们将不胜感激!
主数据网格
<DataGrid x:Name="PostDataGrid"
ItemsSource="{Binding WSEDriverList}"
SelectedItem="{Binding SelectedWSEDriverPOST}"
Style="{DynamicResource MainDataGridStyle}"
Margin="0,0,0,0"
Grid.Row="1" >
<DataGrid.Columns>
<DataGridTextColumn Header="HesId" Binding="{Binding HESID, Mode=OneWay}" Width="50"/>
<DataGridTextColumn Header="WseId" Binding="{Binding WSEID, Mode=OneWay}" Width="50"/>
<DataGridTextColumn Header="API" Binding="{Binding API, Mode=OneWay}" Width="198" />
<DataGridTextColumn Header="Request Date" Binding="{Binding Path=REQUEST_DATE, Mode=OneWay, StringFormat=dd/MM/yyyy HH:mm:ss}" Width="125"/>
<DataGridTextColumn Header="Result Date" Binding="{Binding Path=RESULT_DATE, Mode=OneWay, StringFormat=dd/MM/yyyy HH:mm:ss}" Width="125"/>
<DataGridTextColumn Header="Return Code" Binding="{Binding RETURN_CODE, Mode=OneWay, StringFormat=0}" Width="80" />
<DataGridTextColumn Header="Status" Binding="{Binding STATUS, Mode=OneWay, StringFormat=0}" Width="70" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate DataType="{x:Type viewmodel:WSEAuditViewModel}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Expander Header="NOTIFICATION" Grid.Row="0">
<ItemsControl ItemsSource="{Binding WSEDriverGETResult}">
<usercontrol:NOTIFICATIONUserControl/>
</ItemsControl>
</Expander>
<Expander Header="GET" Grid.Row="1">
<ItemsControl ItemsSource="{Binding WSEDriverGETResult}">
<usercontrol:GETUserControl/>
</ItemsControl>
</Expander>
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
数据网格 2(通知用户控件)
<DataGrid x:Name="NotificationDG"
ItemsSource="{Binding NotificationResult}"
数据网格 3(获取用户控件)
<DataGrid x:Name="GetDG"
ItemsSource="{Binding WSEDriverGETResult}"
编辑
这是我的 DependencyProperties
绑定到 DataGrid
的代码。
//----POST result list from WSE_DRIVER-----
public List<WSE_DRIVER> WSEDriverList
{
get { return (List<WSE_DRIVER>)GetValue(WSEDriverListProperty); }
set { SetValue(WSEDriverListProperty, value); }
}
public static readonly DependencyProperty WSEDriverListProperty =
DependencyProperty.Register("WSEDriverList",
typeof(List<WSE_DRIVER>),
typeof(WSEAuditViewModel),
new PropertyMetadata(WSEDriverListChanged_Callback));
private static void WSEDriverListChanged_Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
WSEAuditViewModel wse_win = (WSEAuditViewModel)d;
if (wse_win.WSEDriverList.Count > 0)
{
wse_win.SelectedWSEDriverPOST = wse_win.WSEDriverList.First();
}
}
//----Selected POST WSE_Driver----
public WSE_DRIVER SelectedWSEDriverPOST
{
get { return (WSE_DRIVER)GetValue(SelectedWSEDriverPOSTProperty); }
set { SetValue(SelectedWSEDriverPOSTProperty, value); }
}
public static readonly DependencyProperty SelectedWSEDriverPOSTProperty =
DependencyProperty.Register("SelectedWSEDriverPOST",
typeof(WSE_DRIVER),
typeof(WSEAuditViewModel),
new PropertyMetadata(SelectedWSEDriverPOSTChanged_Callback));
private static void SelectedWSEDriverPOSTChanged_Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Gets list and assigns to WSEDriverGETResult and NotificationResult
}
//----GET result from WSE_Driver----
public List<WSE_DRIVER> WSEDriverGETResult
{
get { return (List<WSE_DRIVER>)GetValue(WSEDriverGETResultProperty); }
set { SetValue(WSEDriverGETResultProperty, value); }
}
public static readonly DependencyProperty WSEDriverGETResultProperty =
DependencyProperty.Register("WSEDriverGETResult",
typeof(List<WSE_DRIVER>),
typeof(WSEAuditViewModel),
new PropertyMetadata(WSEDriverGETResultChanged_Callback));
private static void WSEDriverGETResultChanged_Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
//----Notification Info----
public List<SPEC_NOTIFICATION> NotificationResult
{
get { return (List<SPEC_NOTIFICATION>)GetValue(NotificationResultProperty); }
set { SetValue(NotificationResultProperty, value); }
}
public static readonly DependencyProperty NotificationResultProperty =
DependencyProperty.Register("NotificationResult",
typeof(List<SPEC_NOTIFICATION>),
typeof(WSEAuditViewModel),
new UIPropertyMetadata(NotificationResultChanged_Callback));
private static void NotificationResultChanged_Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
由于 NotificationResult
和 WSEDriverGETResult
属性与 WSEDriverList
属性 属于相同的 class,您应该能够使用 {RelativeSource}
绑定到父 DataGrid
:
DataContext
<DataGrid x:Name="NotificationDG" ItemsSource="{Binding DataContext.NotificationResult,
RelativeSource={RelativeSource AncestorType=DataGrid}}"></DataGrid>
<DataGrid x:Name="GetDG" ItemsSource="{Binding DataContext.WSEDriverGETResult,
RelativeSource={RelativeSource AncestorType=DataGrid}}"></DataGrid>