2013-04-08 113 views
0

我的小項目看起來現在完全不同。現在我有ObservableCollection PackagesList數據,我想要綁定與整個DataGrid(通過綁定路徑)和ObservableCollection DestinationItememsSource其中我存儲DataGridComboboxColumn(作爲ItemsSourceBinding)的情況。 DatagridComboboxColumn中的SelectedItem屬性是PackageInfo中的一個值(它是c的DestinationNames個案例之一)。在DatagridTextBoxColumns上綁定是可以的。ObservableCollection與INotifyPropertyChanged到DatagridComboboxColumn綁定

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同步?請幫助一些例子。

回答

0

您綁定到DestinationItememsSource不起作用,因爲它不是PackageInfo對象的一部分。您也無法通過FindAncestor或ElementName綁定綁定到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 ^^

Anotherone是簡單地增加一個。您的PackageInfo對象的目標列表(但這會導致大量冗餘)。

0

Have PackageInfo實現INotifyPropertyChanged。

ObservableCollection自動實現INotifyCollectionChanged。

您將需要添加列表或目的地的ObservableCollection作爲PackageInfo
的屬性NO類DestinationNames
只是一類DestinationName

相關問題