C# WPF绑定到用户控制数据网格

C# WPF绑定到用户控制数据网格,c#,wpf,C#,Wpf,我有一个用户控件,我正试图利用它跨多个实例使用。但我似乎无法使装订正常工作 我拥有的控件XAML: <UserControl x:Class="EveCommon.WPF.Inventory.EveInventoryGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/

我有一个用户控件,我正试图利用它跨多个实例使用。但我似乎无法使装订正常工作

我拥有的控件XAML:

<UserControl x:Class="EveCommon.WPF.Inventory.EveInventoryGrid"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:EveCommon.WPF.Inventory"
         mc:Ignorable="d" 
         x:Name="uc"
         d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
    <Style x:Key="HeaderRightJustify" TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Right"/>
    </Style>
    <Style x:Key="ColumnRight" TargetType="DataGridCell">
        <Setter Property="HorizontalContentAlignment" Value="Right"/>
    </Style>
</UserControl.Resources>
<Grid>
    <DataGrid x:Name="ItemsDG" IsReadOnly="True" ItemsSource="{Binding Path=Inventory, ElementName=uc}" AutoGenerateColumns="False">
        <DataGrid.Columns>
        <!-- SETUP COLUMNS HERE -->
        </DataGrid.Columns>
    </DataGrid>
</Grid>
最后是主窗口的呼叫

<inventory:EveInventoryGrid x:Name="ItemsView" Grid.Column="1" Inventory="{Binding Path=Inventory}"  ShowName="True" ShowCost="True"/>

Inventory是EveInventory的一个实例,并实现INotifyPropertyChanged


我只是看不到/不明白我在这里缺少了什么。

如上所述,当使用带有
INotifyPropertyChanged
的绑定时,您必须使用
ObservableCollection
而不是
List
。否则,系统将无法识别更改,也无法更新UI。

如上所述,在使用带有
INotifyPropertyChanged
的绑定时,您必须使用
ObservableCollection
而不是
List
。否则,系统将无法识别更改,也无法更新UI。

您能否详细说明一下“但我似乎无法使绑定正常工作。”?了解什么不起作用会很有帮助。我可以看到主窗口对象实例被填充,但是UI没有更新信息。您正在呼叫吗?这是在UI中更新值时必须执行的操作。仅仅实现INotifyPropertyChanged是不够的,所以我实现了INotifyPropertyChanged并在主控件上调用它。EveInventory有一个属性Items,该属性Items是一个列表,并且有一个getter/setter,在调用时将引发PropertyChanged。但是,当我更新库存时,似乎没有调用PropertyChanged…对于集合,请查看:
observedcollection
。当绑定到一个集合时,你想使用
observateCollection
而不是一个普通的
列表
你能详细说明一下“但是我似乎无法让绑定正常工作。”?了解什么不起作用会很有帮助。我可以看到主窗口对象实例被填充,但是UI没有更新信息。您正在呼叫吗?这是在UI中更新值时必须执行的操作。仅仅实现INotifyPropertyChanged是不够的,所以我实现了INotifyPropertyChanged并在主控件上调用它。EveInventory有一个属性Items,该属性Items是一个列表,并且有一个getter/setter,在调用时将引发PropertyChanged。但是,当我更新库存时,似乎没有调用PropertyChanged…对于集合,请查看:
observedcollection
。当绑定到一个集合时,您想使用
ObservableCollection
而不是普通的
列表
<inventory:EveInventoryGrid x:Name="ItemsView" Grid.Column="1" Inventory="{Binding Path=Inventory}"  ShowName="True" ShowCost="True"/>