【问题标题】:Setting up a DependencyProperty in the backing store to a UI via a data template - backing store not updated通过数据模板将后备存储中的 DependencyProperty 设置到 UI - 后备存储未更新
【发布时间】:2016-03-01 12:48:00
【问题描述】:

我的后备存储中有以下 DP 包装 InputEnabled 属性:

public EnableDisableSetting InputEnabled
{
    get { return (EnableDisableSetting)this.GetValue(InputEnabledProperty); }
    set { this.SetValue(InputEnabledProperty, value); }
}

public static readonly DependencyProperty InputEnabledProperty = DependencyProperty.Register("InputEnabled", typeof(EnableDisableSetting), typeof(EMSBasicDevice));//, new PropertyMetadata(EnableDisableSettings.Enabled));

我有另一个对象结构,其中填充了我们用来控制 DataGrid 的行和列的各种参数:

public class DeviceDisableEnable 
{
    private EnableDisableSetting _Individual_EnDis;

    public EnableDisableSetting Individual_EnDis
    {
        get { return _Individual_EnDis; }
        set { _Individual_EnDis = value; }
    }
}

(大部分字段省略,仅显示相关字段)

在运行时,此结构会填充来自后备存储的值:

public void LoadDeviceDisable()
{
    DeviceDisableEnable dde;

    // Third Line
    dde = new DeviceDisableEnable(this);
    dde.RowHeight        = 21;
    dde.DataDescription  = this.Inputs[0].Name;
    dde.ZoneText         = InZoneID.ShortName;
    dde.LocationText     = Inputs[0].LocationTexts[0];
    dde.Individual_EnDis = this.Inputs[0].InputEnabled;
    dde.Ind_Enable_Text  = this.Inputs[0].InputEnabled.Description;
    dde.IsHeader         = false;
    dde.ShowIndEnable    = true;
    dde.ShowAllEnable    = true;
    DeviceDisablesList.Add(dde);

所以值 dde.Individual_EnDis 从 DP 获取后备存储值。这可以正常工作。

显示的结构用于在 UI 中构建 DataGrid。结构中的每个条目代表网格中的一列。这是构建与此条目关联的列的代码:

// Add a component for the Enable/Disable property
DataGridTemplateColumn EnableCol = new DataGridTemplateColumn();
Binding IndivBind = new Binding("Individual_EnDis");
IndivBind.Mode    = BindingMode.TwoWay;
IndivBind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
IndivBind.Converter = new EMSDevices.EnabledDisabledConverter();

Binding bind3 = new Binding("IsHeader");
bind3.Mode    = BindingMode.TwoWay;
bind3.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

FrameworkElementFactory dtContent2 = new FrameworkElementFactory(typeof(EMS_Config_Tool.UIComponents.WPF.DeviceDisableWidget));
ImageTemplate = new DataTemplate();

EnableCol.CellTemplate          = ImageTemplate;
EnableCol.Width                 = new DataGridLength(140);
EnableCol.CanUserSort           = false;

ImageTemplate.VisualTree = dtContent2;

dtContent2.SetBinding(EMS_Config_Tool.UIComponents.WPF.DeviceDisableWidget.SetValueProperty, IndivBind);
dtContent2.SetValue(EMS_Config_Tool.UIComponents.WPF.DeviceDisableWidget.TextProperty, Properties.Resources.GroupEditor_Enabled);
dtContent2.SetValue(EMS_Config_Tool.UIComponents.WPF.DeviceDisableWidget.TitleProperty, Properties.Resources.Device_Edit_042);
dtContent2.SetBinding(EMS_Config_Tool.UIComponents.WPF.DeviceDisableWidget.TitleVisibilityProperty, bind3);

EnableCol.CellStyle        = visible_style;

TheDataGrid.Columns.Add(EnableCol);

有点复杂,抱歉,但有一些有趣的显示需求超出了基本的DataGrid,所以这里有一些技巧可以显示正确的东西。重要的是如何绑定到DeviceDisableEnable 结构中的Individual_EnDis 属性。 到目前为止一切顺利,这一切都可以,至少有一种方式,但遗憾的是不是两种方式。通过单击Checkbox 更改 UI 中的值实际上会更新 DDE 结构中的设置 --- 但是 --- 最后是问题,后备存储没有得到更新,所以虽然有一个 DP将属性包装在后备存储中,即 DP 不会通过 DDE 结构“涟漪”以让 UI 更新后备存储,换句话说,使用中间结构仅呈现的 财产,而不是财产本身。显然我需要更改 DDE 结构以正确引用后备存储 DP 属性,但现在如何进行,我不知道。

【问题讨论】:

    标签: c# wpf datagrid dependency-properties


    【解决方案1】:

    哦,天哪.....好吧,我已经回答了我自己的问题,显然我已经混淆了这个问题,以至于无法轻易看到它的明显解决方案。

    如果网格是从中间数据模板填充的,则更改后备存储中的数据不会对中间结构产生任何影响,除非它也被更新。所以,我需要做的就是用新的数据值更新中间结构,并调用 [DataGrid].Items.Refresh() 并更新网格以显示新的数据条目。当然,这可能不是最有效的,因为该事务中的大多数行都没有改变,所以我需要做的只是在受影响的条目上调用 Refresh 方法,我已经知道它们是哪些更新了它们在中间结构中的值。 最有可能的是,使用 iNotifyPropertyChanged 通知我可以在后备存储发生更改时直接刷新中间结构,然后将其传播到 DataGrid。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2014-06-04
      相关资源
      最近更新 更多