Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataTemplate中的DataGrid未更新_C#_Wpf_Datagrid - Fatal编程技术网

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.Second
ItemsSource=“{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;

}