繁体   English   中英

将具有INotifyProperty的ObservableCollection更改为DatagridComboboxColumn绑定

[英]ObservableCollection with INotifyPropertyChanged into DatagridComboboxColumn Binding

我的小项目现在看起来很不一样。 现在,我有了ObservableCollection PackagesList ,其中包含要与整个DataGrid(通过绑定路径)和ObservableCollection DestinationItememsSource绑定的数据,我在其中存储DataGridComboboxColumn的案例(作为ItemsSourceBinding)。 DatagridComboboxColumn中的SelectedItem属性是PackageInfo一个值(它是DestinationNames c之一)。 在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同步,该怎么办? 请帮助一些例子。

您对DestinationItememsSource的绑定无效,因为它不是PackageInfo对象的一部分。 您也无法通过FindAncestor或ElementName绑定绑定到Windows DataContext,因为不会将DataGrid列添加到可视树。

我能想到的一种解决方案是使用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。^^

另一个方法是简单地将Package of Destinations添加到您的PackageInfo对象(但这会导致很多冗余)。

让PackageInfo实现INotifyPropertyChanged。

一个ObservableCollection自动实现INotifyCollectionChanged。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM