C# DataTemplate中的DataGrid未更新
我正在尝试制作一个C# DataTemplate中的DataGrid未更新,c#,wpf,datagrid,C#,Wpf,Datagrid,我正在尝试制作一个wpf应用程序,其中将有一个datagrid,能够单击一行并获取另一个datagrid 为此,我创建了一个模型: public class TestData { public DataTable First { get; set; } public DataTable Second { get; set; } } 创建ViewModel,如下所示: public class ViewModelTestClass : INotifyPropertyChanged
wpf
应用程序,其中将有一个datagrid
,能够单击一行并获取另一个datagrid
为此,我创建了一个模型
:
public class TestData
{
public DataTable First { get; set; }
public DataTable Second { get; set; }
}
创建ViewModel
,如下所示:
public class ViewModelTestClass : INotifyPropertyChanged
{
private TestData _testCollection;
public TestData TestCollection
{
get => _testCollection;
set
{
_testCollection = value;
RaisePropertyChanged("TestCollection");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private DataTable _first;
private DataTable _second;
public ViewModelTestClass(DataTable first, DataTable second)
{
TestCollection = new TestData();
_first = first;
_second = second;
BindData();
}
private void BindData()
{
TestCollection.First = _first;
TestCollection.Second = _second;
}
}
以及视图的某些部分
:
<Window.Resources>
<DataTemplate x:Key="RowDetailTemplate">
<Grid x:Name="RowDetailGrid"
Width="470"
Height="Auto"
Margin="5">
<DataGrid
DataContext="{Binding Path = TestCollection.Second, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
>
</DataGrid>
</Grid>
</DataTemplate>
</Window.Resources>
<DataGrid
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="6"
Grid.RowSpan="2"
Name="StatisticDataGrid"
Margin="5"
ItemsSource="{Binding TestCollection.First}"
CanUserAddRows="False"
ColumnWidth="*"
CanUserSortColumns="False"
MouseDoubleClick="StatisticDataGrid_OnMouseDoubleClick"
RowDetailsTemplate="{StaticResource RowDetailTemplate}"
RowDetailsVisibilityChanged="dataGrid1_RowDetailsVisibilityChanged"
>
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<ToggleButton x:Name="RowHeaderToggleButton"
Click="ToggleButton_Click"
Cursor="Hand" />
</DataTemplate>
</DataGrid.RowHeaderTemplate>
</DataGrid>
第一个DataGrid填充得很好,但当我单击一行时,第二个DataGrid
什么也不显示,尽管第二个表已满且不是Null
我做错了什么以及如何处理它?rowDetailTemplate中的datagrid应该是
<DataGrid
DataContext="{Binding Path = DataContext.TestCollection.Second,RelativeSource={RelativeSource AncestorType=DataGrid,Mode=FindAncestor}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding}" >
</DataGrid>
[CallMemberName]将自动设置参数,因此您无需发送参数
其次,如果您想在代码隐藏中动态更改数据表
Testdata应实现INotifyPropertyChanged
您需要在第一个和第二个属性上引发已更改的属性
public class TestData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public DataTable First
{
get { return _First; }
set
{
_First = value;
RaisePropertyChanged();
}
}
private DataTable _First;
public DataTable Second
{
get { return _Second; }
set
{
_Second = value;
RaisePropertyChanged();
}
}
private DataTable _Second;
}
最后,当绑定不起作用时,您可以检查来自调试的VisualStudio输出
它将告诉您绑定失败的原因行是否有名为
TestCollection.Second的属性
?不,它们是数据表TestCollection.Second
是viewmodel的成员。Row detail是行的详细信息:RowDetailsTemplate中的DataContext是主数据网格中的父行。此外,您正在尝试将DataContext
设置为集合,而不是ItemsSource
…因此您需要了解行详细信息的内容,以及如何在需要的位置获取行详细信息。您是否正在更新外部网格的SelectionChanged事件中的TestCollection.SecondItemsSource=“{Binding DataContext.TestCollection.Second,RelativeSource={RelativeSource AncestorType=DataGrid}}”
应该可以,我相信。它可以,谢谢!我还有一个问题。如何将另一个datagrid添加到datatemplate中的第二个网格中?您可以创建其他rowdatatemplate并设置datagrid的rowdatatemplate,它位于原始rowdatatemplate中。
private TestData _testCollection;
public TestData TestCollection
{
get => _testCollection;
set
{
_testCollection = value;
RaisePropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public class TestData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public DataTable First
{
get { return _First; }
set
{
_First = value;
RaisePropertyChanged();
}
}
private DataTable _First;
public DataTable Second
{
get { return _Second; }
set
{
_Second = value;
RaisePropertyChanged();
}
}
private DataTable _Second;
}