简体   繁体   English

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

[英]WPF binding to user control data grid

I have a user control which I'm trying to leverage for usage across many instances. 我有一个用户控件,我试图利用它在许多实例中使用。 But I can't seem to get the binding to work properly. 但是我似乎无法使绑定正常工作。

The control XAML I have: 我拥有的控件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>

And the code behind: 以及后面的代码:

public partial class EveInventoryGrid : UserControl
{
    public static DependencyProperty InventoryProperty = DependencyProperty.Register(
        "Inventory", 
        typeof(EveInventory),
        typeof(EveInventoryGrid));

    public EveInventory Inventory
    {
        get { return (EveInventory)GetValue(InventoryProperty); }
        set { SetValue(InventoryProperty, value); }
    }
}

And lastly the calling from the Mainwindow. 最后是来自主窗口的呼叫。

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

Inventory is an instance of EveInventory, and does implement INotifyPropertyChanged. 库存是EveInventory的一个实例,并且确实实现了INotifyPropertyChanged。

I just don't see/understand what I'm missing here. 我只是看不到/不明白我在这里缺少什么。

As explained above, you'll have to use ObservableCollection<T> instead of List<T> when using Bindings with INotifyPropertyChanged . 如上所述,在将绑定与INotifyPropertyChanged一起使用时,必须使用ObservableCollection<T>而不是List<T> Otherwise the System will not recognize the change and not update the UI. 否则,系统将无法识别更改并且不会更新UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM