Silverlight DataGrid更新绑定数据时,如何更改?

| 当我离开一个单元格时,我的
DataGrid
(Silverlight 4)正在更新。每当单元格的值更改时,我都需要更新它。     
已邀请:
我得出了自己的答案,它类似于用于立即更改TextBox的绑定源的行为注入(请参见此处)。我将ѭ0子类化,并添加了以下代码:
    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();
    }
    
只需在您设置datagrid的itemssource的类上实现INotifyPropertyChanged。 例如
 public class CustomType:INotifyPropertyChanged
 {

 }

 List<CustomType> list=new List<CustomType>();
添加项目
datagrid.ItemsSource=list;
绑定模式=双向     

要回复问题请先登录注册