【问题标题】:SetValue makes dependencyProperty nullSetValue 使 dependencyProperty 为空
【发布时间】:2015-08-28 07:36:57
【问题描述】:

我试图在我的依赖属性上设置一个值,但它总是设置为 null。

[Description("Binded destination list"), Category("Data")]
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("DestinationList", typeof(IEnumerable<TestEntity>), typeof(ListBoxEditLookup), new FrameworkPropertyMetadata(IsDestinationListChangedCallback) { BindsTwoWayByDefault = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged});

public IEnumerable<TestEntity> DestinationList
{
     get { return GetValue(ItemsProperty) as IEnumerable<TestEntity>; }
     set
     {
          //After this line it becomes null
          SetValue(ItemsProperty, value);
     }
}

当我检查 value 的值时,它实际上填充了 IEnumerable&lt;TestEntity&gt; 类型的值,但由于某种原因它显示为 null!当我设置所有类型的对象而不是 IEnumerable 时,它​​就可以工作了。

【问题讨论】:

  • 你什么时候检查房产?它可能为空,因为您检查它为时过早。也许您可以在控件完成加载后检查它。
  • 控件已加载,当我选中一个框时我更改了值,所以这不是问题。视图模型的初始值也设置正确。只有当我更改控件内部的值时才会出错!

标签: c# wpf dependency-properties


【解决方案1】:

我发现了问题,因为我绑定了 ObservableCollection,所以不能强制转换为 IEnumerable

【讨论】:

  • 我有同样的问题,并且通过 AsEnumerable() 将 ObservableCollection 转换为 IEnumerable 救了我)
猜你喜欢
  • 2011-09-08
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多