微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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;
绑定模式=双向     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。