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

kw1jybo

我有一个用户控件,我试图利用它在许多实例中使用。但是我似乎无法使绑定正常工作。

我拥有的控件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>

以及后面的代码:

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); }
    }
}

最后是来自主窗口的呼叫。

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

库存是EveInventory的一个实例,并且确实实现了INotifyPropertyChanged。

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

汤姆·斯坦

如上所述,在与结合使用,您必须使用ObservableCollection<T>代替否则,系统将无法识别更改并且不会更新UI。List<T>INotifyPropertyChanged

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章