0

我有一个应用程序,其中包含一个ObservableCollection<Foo>,而Foo反过来又包含一个ObservableCollection<Bar>. 我想要一对数据网格,一个显示Foo应用程序中的对象集合,另一个显示当前在第一个数据网格中选择的Bar对象集合Foo(并且我希望能够在两者中添加、更新和删除条目数据网格)。

到目前为止,我有以下 XAML:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <DataGrid Grid.Column="0" ItemsSource="{Binding Foos}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Foo Name" Binding="{Binding Name}" Width="Auto" IsReadOnly="False" />
            </DataGrid.Columns>
        </DataGrid>
        <GridSplitter HorizontalAlignment="Right" VerticalAlignment="Stretch" Grid.Column="1" ResizeBehavior="PreviousAndNext" Width="5" Background="Gray" />
        <DataGrid Grid.Column="2">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Bar Name" Width="Auto" IsReadOnly="False"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

以及以下代码:

using System;
using System.Windows;
using System.Collections.ObjectModel;

namespace Test
{
    public class Foo
    {
        static int _nextId;
        public int Id { get; private set; }
        public String Name { get; set; }
        public ObservableCollection<Bar> Bars { get; set; }
        public Foo()
        {
            Id = _nextId++;
            Name = String.Empty;
            Bars = new ObservableCollection<Bar>();
        }
    }

    public class Bar
    {
        static int _nextId;
        public int Id { get; private set; }
        public String Name { get; set; }
        public Bar()
        {
            Id = _nextId++;
            Name = String.Empty;
        }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<Foo> Foos { get; set; }
        public MainWindow()
        {
            Foos = new ObservableCollection<Foo>();
            Foo newFoo;
            for (int index = 0; index < 5; ++index)
            {
                newFoo = new Foo();
                newFoo.Name = String.Format("Foo {0}", index);
                Foos.Add(newFoo);
            }
            InitializeComponent();
            DataContext = this;
        }
    }
}

显然我还没有绑定第二个 DataGrid,因为我一点也不知道该怎么做!我能找到的所有示例都假设我正在绑定 DataTables,而不是自定义对象,并绑定到 DataTables 上的关系。我还不太了解绑定。谁能告诉我如何绑定第二张桌子?

(是的,如果你看过我最近提出的其他问题,我会在早期与 WPF 相处得不好之后再给它一次机会)。

提前致谢。

4

2 回答 2

1

使用绑定到元素
名称顶部网格 gridTop

DataContext="{Binding ElementName=gridTop, Path=SelectedItem.Bars}"
于 2013-07-23T22:41:03.370 回答
1

嗨,如果您首先想要可编辑的网格,则必须像这样实现 INotifyPropertyChanged

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

public class ViewModel 
{
    public ViewModel()
    {
        Foos = new ObservableCollection<Foo>();
    }

    public ObservableCollection<Foo> Foos { get; set; }
}

public class Foo : INotifyPropertyChanged
{
    static int _nextId;
    public int Id { get; private set; }
    public ObservableCollection<Bar> Bars { get; set; }
    public Foo()
    {
        Id = _nextId++;
        Name = String.Empty;
        Bars = new ObservableCollection<Bar>();
    }
    private string name;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            Notify("Name");
        }
    }

    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

}

public class Bar : INotifyPropertyChanged
{
    static int _nextId;
    public int Id { get; private set; }
    public Bar()
    {
        Id = _nextId++;
        Name = String.Empty;
    }

    private string name;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            Notify("Name");
        }
    }

    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

在第一个网格的 xaml 绑定中是正确的,对于第二个网格,您可以使用 ElementName 将 ItemsSource 设置为第一个网格的 selectedItem

<DataGrid Grid.Column="2" ItemsSource="{Binding ElementName=gridTop, Path=SelectedItem.Bars}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Bar Name" Binding="{Binding Name}" Width="Auto" IsReadOnly="False"/>
        </DataGrid.Columns>
    </DataGrid>
于 2013-07-23T23:21:58.663 回答