1

我有 3 个模型:机器、零件和组件。在我的用户界面上,我将一个列表框绑定到机器列表,将第二个列表框绑定到所选机器的零件列表。现在,当用户选择一个零件时,我想将一个数据网格绑定到所选零件的组件列表。我尝试将数据网格的 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>
4

1 回答 1

1

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

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

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

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

于 2013-02-07T20:26:18.477 回答