C# 从两个计数相同的集合中选择Wpf DataGrid绑定

C# 从两个计数相同的集合中选择Wpf DataGrid绑定,c#,wpf,data-binding,wpfdatagrid,C#,Wpf,Data Binding,Wpfdatagrid,我有列表F和列表B,计数相同。 在WPF和mantain中,将列F.x和B.y绑定到同一个DataGrid的最干净/最好的方法是什么?从DataGrid到列表F和B的单向绑定。我将创建一个混合对象: 课程包括: public class FooBar { public Foo F { get; set; } public Bar B { get; set; } } public class Foo { pu

我有
列表F
列表B
,计数相同。
在WPF和mantain中,将列F.x和B.y绑定到同一个DataGrid的最干净/最好的方法是什么?从DataGrid到列表F和B的单向绑定。

我将创建一个混合对象:

课程包括:

    public class FooBar
    {
        public Foo F { get; set; }
        public Bar B { get; set; }
    }

    public class Foo
    {
        public int X { get; set; }
    }

    public class Bar
    {
        public int Y { get; set; }
    }
视图模型:

    public class ViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Implementation

        public event PropertyChangedEventHandler PropertyChanged;

        protected void InvokePropertyChanged(string propertyName)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            if (PropertyChanged != null) PropertyChanged(this, e);
        }

        #endregion

        public ViewModel()
        {
            this.FooBars.Add(new FooBar()
            {
                B = new Bar(){Y = 30},
                F = new Foo(){ X = 100}
            });
        }

        private ObservableCollection<FooBar> fooBars = new ObservableCollection<FooBar>();
        public ObservableCollection<FooBar> FooBars
        {
            get { return this.fooBars; }
            set
            {
                this.fooBars = value;
                InvokePropertyChanged("FooBars");
            }
        }
    }
观点:

        <DataGrid ItemsSource="{Binding FooBars}" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=F.X, Mode=TwoWay}" Header="This is FOO"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Path=B.Y, Mode=TwoWay}" Header="This is BAR"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>


希望这有助于详细解释Thx。我将尝试使用此解决方案的一部分来启用表单列表和列表的单向绑定。
        <DataGrid ItemsSource="{Binding FooBars}" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=F.X, Mode=TwoWay}" Header="This is FOO"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Path=B.Y, Mode=TwoWay}" Header="This is BAR"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>