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



我有三种型号:Machine、Part和Component。在我的ui上,我将一个列表框绑定到机器列表,将第二个列表框添加到所选机器的零件列表。现在,当用户选择一个零件时,我想将数据网格绑定到所选零件的组件列表。我尝试将数据网格的项目源设置为:

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

我还尝试设置它的数据上下文和项目源

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="..ResourcesImagesdelete.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="..ResourcesImagesdelete.png" Stretch="Uniform"/>
                        </Button>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
</Grid>
</Page>

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

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

或者您可以手动实现界面INotifyCollectionChanged

基本上,问题是绑定系统不知道您何时调用Add。如果您每次都重新分配Parts属性,那么系统只会重新绘制所有内容,因此它也会反映更改,但如果ObservableCollection不是问题,这是

的首选方式

相关内容

最新更新