WPF:嵌套绑定的正确方法是什么

时间:2013-02-07 19:54:54

标签: c# wpf data-binding

我有3个型号:机器,零件和组件。在我的UI上,我将一个列表框绑定到一个机器列表,并将第二个列表框绑定到所选机器的部件列表。现在,当用户选择一个部件时,我想将一个数据网格绑定到所选部件的组件列表。我尝试将datagrid的itemssource设置为:

ItemsSource="{Binding ElementName=PartSelectList, Path=SelectedItem.Components}"

我也尝试过设置datacontext和itemssource

DataContext={Binding ElementName=PartSelectList, Path=SelectedItem}

ItemsSource={Binding Components}

但是,如果通过诸如

之类的方法对部件的组件列表进行更改,则数据网格不会自动更新

part.Bags.Add(new Component(..));

任何人都有关于如何处理这个问题的建议吗?

更新:我有一个实现INotifyPropertyChanged的类MachineCollection。在我的xaml.cs代码中,我创建了我的MachineCollection对象并将MainGrid绑定到它的Machines属性:

Page1.xaml.cs:

private MachineCollection machines

public Page1()
{
    machineCollection = new MachineCollection();
    MainGrid.DataContext = machineCollection.Machines 
    actionThread = new Thread(InitLists);
    actionThread.Start();
}

public void InitLists()
{
    machineCollection.Machines.AddRange(dbCon.GetAllMachines());
}

类:

public class MachineCollection : INotifyPropertyChanged
{
    public List<Machine> Machines
    {
        get { return machines; }
        set { machines = value; NotifyPropertyChanged("Machines"); }
    }
}

public class Machine : INotifyPropertyChanged
{
    public List<Part> Parts
    {
        get { return parts; }
        set { parts = value; NotifyPropertyChanged("Parts");
    }
}

public class Part : INotifyPropertyChanged
{
    public List<Component> Components
    {
        get { return components; }
        set { components = value; NotifyPropertyChanged("Components");
    }
}

public class Component : INotifyPropertyChanged
{
    public int Length
    {
        get { return length; }
        set { length = value; NotifyPropertyChanged("Length");
    }
}

XAML:

<Page>
<Grid Name="MainGrid">
    <ListBox x:Name="MachineSelectList" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
                 Height="114"  Width="231"
                 Foreground="White" Background="#FF7A7A7A" FontSize="16" BorderBrush="#FFC13131"
                 ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="200">
                        <TextBlock Text="{Binding SerialNumber}" TextAlignment="Left" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                        <Button Name="DeleteMachine" Tag="{Binding SerialNumber}" HorizontalAlignment="Right" VerticalAlignment="Center" Height="20"
                                Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}" Focusable="False" Click="Btn_Click" >
                            <Image Source="..\Resources\Images\delete.png" Stretch="Uniform"/>
                        </Button>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    <ListBox Name="PartSelectList" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
                 Height="164"  Width="231"
                 Background="#FF7A7A7A" Foreground="White" FontSize="16" BorderBrush="#FFC13131"
                 ItemsSource="{Binding Path=Parts}" IsSynchronizedWithCurrentItem="True">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="200">
                        <TextBlock Text="{Binding PartNumber}" TextAlignment="Left" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                        <Button Name="DeletePart" Tag="{Binding PartNumber}" HorizontalAlignment="Right" VerticalAlignment="Center" Height="20"
                                Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}" Focusable="False" Click="Btn_Click" >
                            <Image Source="..\Resources\Images\delete.png" Stretch="Uniform"/>
                        </Button>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

</Grid>
</Page>

1 个答案:

答案 0 :(得分:1)

解决此问题的一种方法是使用ObservableCollection,它是针对这种情况而设计的。

public ObservableCollection<Part> Parts
{
    get { return parts; }
    set { parts = value; NotifyPropertyChanged("Parts");
}

或者您可以手动实现接口INotifyCollectionChanged

基本上,问题是绑定系统不知道何时调用Add。如果你每次都重新分配Parts属性,那么系统只会重绘所有内容,所以它也会反映出这些变化,但是如果ObservableCollection不是问题,那就是首选方式