首页 > 解决方案 > C# WPF 在 SelectedItem 上从另一个 DataGrid 绑定 DataGrid

问题描述

我有两个 DataGrid 元素。第一个加载了文章数据,第二个应该根据第一个 DataGrid 的 SelectedItem 显示更多数据。

第一个 DataGrid 与文章数据的绑定工作完美。

现在我有第二个,它应该根据第一个的选择显示更多数据。在 ViewModel.cs 我创建了以下内容:

    public BindableCollection<ArticleDataModel> ArticleDataList {
        get; set;
    }

    public BindableCollection<QuantityModel> WarehouseQuantityList {
        get; set;
    }


    private ArticleDataModel currentSelection;

    public ArticleDataModel CurrentSelection {
        get {
            return currentSelection;
        }
        set {
            currentSelection = value;
            NotifyOfPropertyChange(() => currentSelection);
            this.LoadQuantity();
        }
    }

第一个 DatagridView 绑定到ArticleDataList,所有数据都正确显示。选择数据集时,我可以在调试器中看到调用了 LoadQuantity() 方法并且 WarehouseQuantityList 包含所有数据。

但是,数据没有显示给我,甚至没有显示记录行。

XAML 中的绑定如下所示:

<!-- Artikel ListView -->
<DataGrid Grid.Column="0" Grid.Row="0"
    x:Name="ArticleDataList"
    AutoGenerateColumns="False"
    AlternatingRowBackground="#fafafa"
    FontSize="14"
    RowBackground="#eee"
    HorizontalGridLinesBrush="#ddd"
    VerticalGridLinesBrush="#ddd"
    RowHeaderWidth="0"
    SelectionMode="Single"
    SelectionUnit="FullRow"
    SelectedItem="{Binding CurrentSelection}">

<DataGrid x:Name="WarehouseQuantityList"
    AutoGenerateColumns="False"
    AlternatingRowBackground="#fafafa"
    FontSize="14"
    RowBackground="#eee"
    HorizontalGridLinesBrush="#ddd"
    VerticalGridLinesBrush="#ddd"
    RowHeaderWidth="0"
    SelectionMode="Single"
    SelectionUnit="FullRow">

绑定两次都超过x:Name,项目绑定如下:

<DataGridTextColumn Header="Lagerplatz" Width="250" Binding="{Binding Path=Lagerplatz}"/>

两者的绑定在技术上是相同的,但我没有收到任何错误或异常。加载的数据是正确的,并且在 WarehouseQuantityList 中也可用。只有 DataGrid 看不到它们。所以我认为我犯了一个错误,只是不知道在哪里?

我总是乐于接受提示:-)

标签: c#wpfbindingdatagridcaliburn.micro

解决方案


推荐阅读