2011-04-15 133 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

相关问题