【问题标题】:ObservableCollection with INotifyPropertyChanged into DatagridComboboxColumn BindingObservableCollection 与 INotifyPropertyChanged 成 DatagridComboboxColumn 绑定
【发布时间】:2013-03-30 18:27:04
【问题描述】:

我的小项目现在看起来完全不同了。现在我有 ObservableCollection PackagesList 和我想与整个 DataGrid 绑定的数据(通过绑定路径)和 ObservableCollection DestinationItememsSource 我在其中存储 DataGridComboboxColumn 的案例(作为 ItemsSourceBinding)。 DatagridComboboxColumn 中的 SelectedItem 属性是来自PackageInfo 的一个值(它是DestinationNames 的一种情况)。绑定 DatagridTextBoxColumns 没问题。

XAML:

<Window x:Class="test01.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="400" Loaded="Window_Loaded">
    <Grid>
        <Border x:Name="mainBorder" Margin="20">
            <DataGrid x:Name="mainDataGrid" x:Uid="mainDataGrid" AutoGenerateColumns="False"
                       AlternationCount="2" SelectionMode="Single" HorizontalAlignment="Stretch">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path=id}"
                                    Header="ID" Width="Auto" IsReadOnly="True"/>
                    <DataGridTextColumn Binding="{Binding Path=name}"
                                    Header="Name" Width="Auto" IsReadOnly="True"/>
                    <DataGridTextColumn Binding="{Binding Path=quantity}"
                                    Header="Quantity" Width="Auto" IsReadOnly="True"/>
                    <DataGridTextColumn Binding="{Binding Path=price}"
                                    Header="Price" Width="Auto" IsReadOnly="True"/>
                    <DataGridComboBoxColumn ItemsSource="{Binding DestinationItemsSource}" 
                                                    SelectedItemBinding="{Binding Path=destination, UpdateSourceTrigger=PropertyChanged}"
                                                    Header="Destination" Width="Auto"/>
                </DataGrid.Columns>
            </DataGrid>
        </Border>
    </Grid>
</Window>

C#:

public class PackageInfo
    {
        public int id { get; set; }
        public string name { get; set; }
        public int quantity { get; set; }
        public double price { get; set; }
        public string destination { get; set; }
    }

    public class DestinationNames : INotifyPropertyChanged
    {
        public string name { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public DestinationNames(string value)
        {
            this.name = value;
        }

        public string DestinationName
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged("DestinationName");
            }
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }


    public partial class MainWindow : Window
    {
        public ObservableCollection<DestinationNames> DestinationItememsSource { get; set; }
        public ObservableCollection<PackageInfo> PackagesList { get; set; }

        public MainWindow()
        {
            InitializeComponent();
        }

        public void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadPackages();
        }

        public void LoadPackages()
        {
            DestinationItememsSource = new ObservableCollection<DestinationNames>();
            DestinationItememsSource.Add(new DestinationNames("London"));
            DestinationItememsSource.Add(new DestinationNames("Plymouth"));
            DestinationItememsSource.Add(new DestinationNames("Birmingham"));
            DestinationItememsSource.Add(new DestinationNames("Cambridge"));

            PackagesList = new ObservableCollection<PackageInfo>();
            PackagesList.Add(new PackageInfo { id = 1, name = "Potato", quantity = 3, price = 2.2, destination = "London" });
            PackagesList.Add(new PackageInfo { id = 2, name = "Tomato", quantity = 5, price = 3.8, destination = "Plymouth" });
            PackagesList.Add(new PackageInfo { id = 3, name = "Carrot", quantity = 1, price = 5.1, destination = "London" });
            PackagesList.Add(new PackageInfo { id = 4, name = "Pea", quantity = 6, price = 1.8, destination = "Plymouth" });
            PackagesList.Add(new PackageInfo { id = 5, name = "Bean", quantity = 2, price = 1.5, destination = "Birmingham" });
            mainDataGrid.ItemsSource = PackagesList;
        }
    }

如何正确绑定这个DatagridComboboxColumn?我应该使用 INotifyCollectionChanged 吗?如果我想让 datagrid 中的所有数据自动与 ObservableCollection 同步怎么办?请帮忙举个例子。

【问题讨论】:

    标签: c# wpf data-binding datagrid inotifypropertychanged


    【解决方案1】:

    让 PackageInfo 实现 INotifyPropertyChanged。

    ObservableCollection 自动实现 INotifyCollectionChanged。

    您需要将 List 或 ObservableCollection Destinations 添加为 PackageInfo 的属性
    没有类 DestinationNames
    只是一个类 DestinationName

    【讨论】:

      【解决方案2】:

      您对 DestinationItememsSource 的绑定不起作用,因为它不是 PackageInfo 对象的一部分。您也无法通过 FindAncestor 或 ElementName-binding 绑定到 Windows DataContext,因为 DataGrid-Columns 不会添加到可视化树中。

      我能想到的一个解决方案是使用CollectionViewSource

      <Window.Resources>
          <CollectionViewSource Source="{Binding RelativeSource={RelativeSource Self}, Path=DestinationItememsSource}" x:Key="destinations">
              <!--here you can also add Group and SortDescriptions-->
          </CollectionViewSource>
      </Window.Resources>
      <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource ResourceKey=destinations}}".../> 
      

      atm 无法测试,因为我还在下载 VS 2012。^^

      另一种方法是简单地将目的地列表添加到您的 PackageInfo 对象(但这会导致大量冗余)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-24
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        • 2014-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多