2011-04-15 135 views

回答

1

我來到了我自己的答案和它與用於瞬間改變一個TextBox的綁定行爲注射來源(see here)。我subclassed DataGrid並添加了以下代碼:

protected override void OnPreparingCellForEdit(DataGridPreparingCellForEditEventArgs e) 
    { 
     base.OnPreparingCellForEdit(e); 

     TextBox textBox = e.EditingElement as TextBox; 
     if (textBox != null) 
     { 
      textBox.TextChanged -= OnTextChanged; 
      textBox.TextChanged += OnTextChanged; 
     } 

     ComboBox comboBox = e.EditingElement as ComboBox; 
     if (comboBox != null) 
     { 
      comboBox.SelectionChanged -= OnSelectionChanged; 
      comboBox.SelectionChanged += OnSelectionChanged; 
     } 
    } 

    private void OnSelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     ComboBox comboBox = sender as ComboBox; 

     if (comboBox == null) 
      return; 

     BindingExpression expression = comboBox.GetBindingExpression(ComboBox.SelectedValueProperty); 
     if (expression != null) 
      expression.UpdateSource(); 

     expression = comboBox.GetBindingExpression(ComboBox.SelectedItemProperty); 
     if (expression != null) 
      expression.UpdateSource(); 
    } 

    private void OnTextChanged(object sender, TextChangedEventArgs e) 
    { 
     TextBox textBox = sender as TextBox; 

     if (textBox == null) 
      return; 

     BindingExpression expression = textBox.GetBindingExpression(TextBox.TextProperty); 

     if (expression == null) 
      return; 

     expression.UpdateSource(); 
    } 
0

只是在你設置datagrid的itemssource的類上實現INotifyPropertyChanged。

例如

public class CustomType:INotifyPropertyChanged 
{ 

} 

List<CustomType> list=new List<CustomType>(); 

添加項目

datagrid.ItemsSource=list; 

綁定模式=雙向

+0

這並沒有回答我的問題。我已經在使用'INotifyPropertyChanged'。當網格改變時,我需要改變發生在相反的方向,對象也應該改變。瞬間,而不必離開牢房。 – Jordan 2011-04-15 19:22:33

+0

如果綁定模式設置TwoWay – Nario 2011-04-15 19:30:58

+0

{綁定路徑= PropertyName,Mode = TwoWay} – Nario 2011-04-15 19:31:31

相關問題