2013-01-23 53 views
0

我已經定義了下面的XAML。該綁定填充了DataGridComboBoxColumn,如果我同時在組合框中選擇PartNumber,則需要在DataGridTextColumn中綁定另一個屬性(PartName)值。我怎麼能在DataGridTextColumn中做這個綁定?如何在Datagrid中定義依賴關係綁定

<Window.Resources> 
    <ObjectDataProvider x:Key="RecordValues" 
         ObjectType="{x:Type local:RecordTemp}" 
         MethodName="GetPersonList"> 
    </ObjectDataProvider> 
</Window.Resources> 
<Grid> 
    <Grid> 
    <DataGrid AutoGenerateColumns="False" 
       ItemsSource="{Binding}" 
       Margin="10" 
       Name="dataGrid1"> 
     <DataGrid.Columns> 
     <DataGridComboBoxColumn Header="Combo" 
           Width="300" 
           SelectedItemBinding="{Binding Values}" 
           DisplayMemberPath="PartNumber" 
           ItemsSource="{Binding Source={StaticResource RecordValues}}" /> 
     <DataGridTextColumn Header="Order Name" 
          Binding="" /> 
     </DataGrid.Columns> 
    </DataGrid> 
    </Grid> 
</Grid> 

在下面的代碼定義的C#,要完成這一

ObservableCollection<RecordTemp> RecordsTemp = new ObservableCollection<RecordTemp>(); 

    RecordsTemp.Add(new RecordTemp()); 
    RecordsTemp.Add(new RecordTemp()); 
    dataGrid1.DataContext = RecordsTemp; 


    public class RecordTemp 
    { 
    List<PartsList> _value = new List<PartsList>(); 

     public RecordTemp() 
    { 
     _value.Add(new PartsList() { PartName = "One", PartNumber = "1", PartQuantity = 20 }); 
     _value.Add(new PartsList() { PartName = "Two", PartNumber = "2", PartQuantity = 10 }); 
    } 

    public List<PartsList> value 
    { 
     get { return _value; } 
     set { _value = value; } 
    } 

    public List<PartsList> GetPersonList() 
    { 
     return _value; 
    } 
    } 

public class PartsList 
    { 
     public string PartName { get; set; } 
     public double PartQuantity { get; set; } 
     public string PartNumber { get; set; } 
    } 

回答

1

一種方法是

  1. 添加在RecordTemp類一個新的屬性(比如屬性名是SelectedPart),這將存儲選定的PartList obj。

  2. 將SelectedPart屬性綁定到ComboBox的SelectedValue屬性。

  3. 將SelectedPart綁定到文本框。

  4. 現在,RecordTemp類應實現INotifyPropertyChanged接口,以便在用戶更改ComboBox值時更新UI。

以下是我對代碼所做的修改。

<DataGrid AutoGenerateColumns="False" 
      ItemsSource="{Binding}" 
      Margin="10" 
      Name="dataGrid1" 
      CanUserAddRows="False"> 
    <DataGrid.Columns> 
    <DataGridTemplateColumn Header="Combo"> 
     <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <ComboBox SelectedValue="{Binding SelectedPart, UpdateSourceTrigger=PropertyChanged}" 
        DisplayMemberPath="PartNumber" 
        ItemsSource="{Binding Source={StaticResource RecordValues}}" /> 
     </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    <DataGridTextColumn Header="Order Name" 
         Binding="{Binding SelectedPart.PartName}" /> 
    </DataGrid.Columns> 
</DataGrid> 

代碼在文件後面。您可能必須更改名稱空間。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      ObservableCollection<RecordTemp> RecordsTemp = new ObservableCollection<RecordTemp>(); 

      RecordsTemp.Add(new RecordTemp()); 
      RecordsTemp.Add(new RecordTemp()); 
      dataGrid1.DataContext = RecordsTemp; 
     } 
    } 

    public class RecordTemp : INotifyPropertyChanged 
    { 
     List<PartsList> _value = new List<PartsList>(); 

     public RecordTemp() 
     { 
      _value.Add(new PartsList() { PartName = "One", PartNumber = "1", PartQuantity = 20 }); 
      _value.Add(new PartsList() { PartName = "Two", PartNumber = "2", PartQuantity = 10 }); 
     } 

     public List<PartsList> value 
     { 
      get { return _value; } 
      set { _value = value; } 
     } 

     private PartsList _SelectedPart; 

     public PartsList SelectedPart 
     { 
      get { return _SelectedPart; } 
      set 
      { 
       _SelectedPart = value; 
       OnPropertyChanged("SelectedPart"); 
      } 
     } 

     public List<PartsList> GetPersonList() 
     { 
      return _value; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 

    public class PartsList 
    { 
     public string PartName { get; set; } 
     public double PartQuantity { get; set; } 
     public string PartNumber { get; set; } 
    } 
} 
+0

優秀的一個..謝謝 – Smaug