【问题标题】:Custom UserControl "IsEnabled" data binding not working自定义用户控件“IsEnabled”数据绑定不起作用
【发布时间】:2011-03-17 11:50:18
【问题描述】:

我的 WPF 应用程序现在有一个非常糟糕的问题...

我有一个自定义 UserControl 用于编辑组件的详细信息。它应该从不启用开始,并在用户选择要编辑的组件后立即启用。

问题是:IsEnabled 属性甚至没有改变。

这是我的代码:

<my:UcComponentEditor Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  
                        IsEnabled="{Binding EditorEnabled}"
                              DataContext="{Binding VmComponent}" />

EditorEnabled 是我的 ViewModel (VmComponent) 中的一个属性,默认为 false,当用户选择一个组件或创建一个组件时变为 true

仅作记录,在我的 ViewModel 中:

private Boolean _editorEnabled = false;

    public Boolean EditorEnabled
    {
        get { return _editorEnabled; }
        set 
        {
            _editorEnabled = value;
            OnPropertyChanged("EditorEnabled");
        }
    }

当我尝试启动我的应用程序时,UserControl 正在启动...已启用。 我到处都加了断点,EditorEnabled从一开始就是假的。

我还做了一件非常愚蠢的事情来试图弄清楚发生了什么:我创建了一个转换器(非常有用 - 将布尔值转换为布尔值 - 嗯),在其上放置一个断点,然后......代码是从未到达。

<my:UcComponentEditor Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  
                        IsEnabled="{Binding EditorEnabled, Converter={StaticResource BoolConverter}}"
                              DataContext="{Binding VmComponent}" />

这可能意味着永远不会设置属性 isEnabled,因为永远不会到达转换器。

你觉得那里有什么问题吗?我大约一周前开始在 WPF 工作,因此我可能错过了一些重要的东西......

非常感谢您的宝贵时间 :-)

【问题讨论】:

  • 断点是否停在_editorEnabled = value;
  • 是否正确创建了 VmComponent?据我所知,该绑定不会初始化新对象。
  • @Fun Mun Pieng:是的,它通过 setter

标签: c# wpf mvvm binding isenabled


【解决方案1】:

您应该添加一个 DependencyProperty 以使绑定正常工作。 See here for more information.

代码隐藏:

public static readonly DependencyProperty EditorEnabledDependencyProperty = DependencyProperty.Register("EditorEnabled", typeof(bool), typeof(UcComponentEditor), new PropertyMetadata(false));

public bool EditorEnabled
{
    get { return (bool)base.GetValue(UcComponentEditor.EditorEnabledDependencyProperty); }
    set { base.SetValue(UcComponentEditor.EditorEnabledDependencyProperty, value); }
}

【讨论】:

  • 真的值得加入吗?我的意思是,控件已经自然具有 IsEnabled 属性。我认为添加与 IsEnabled 具有相同角色的 DependencyProperty 会导致编码错误:-/
  • 是的 - XAML 中大量使用依赖属性 - 请参阅我发布的链接以获取更多信息。我用一个例子更新了我的答案。
  • 不,没有必要添加 DependencyProperty - 代码看起来应该可以正常工作。我会尝试显式设置数据上下文 - 或在 VmComponent 属性上更改属性 - 或在 InitializeComponent 之前设置属性值。
  • @Josh M. :嘿,它实际上与设置 IsEnabled 属性的 DependencyProperty 一起使用。但是我发现需要添加这样的属性来处理已经存在的东西非常不清楚。无论如何,谢谢,你的帮助很有用,我会让这个话题打开,看看其他人是否有合理的解释:)
  • 依赖属性处理绑定源和绑定目标之间的双向通信。他们负责通知您的控件该值已更改。如果您将控件直接“绑定”到某个属性,则该控件无法知道该属性何时/是否已更改。唯一可行的方法是它不断地轮询属性的值以查看它是否已更改。依赖属性解决了这个问题,它是实现您发布的内容的正确方法。您或许应该阅读更多关于 Dependency Properties 的内容,但我很高兴它有所帮助。
【解决方案2】:

我认为的问题是用户控件的 DataContext 属性上存在绑定。这意味着 EditorEnabled 属性应该是 VmComponent 对象中的一个属性。至少那是我的问题所在。

为了解决这个问题,我为 IsEnabled 的绑定指定了一个适当的来源。一旦我这样做了,控件就会开始按预期工作。

希望对您有所帮助。

【讨论】:

    【解决方案3】:

    将您的控件封装在 DockPanel 中(例如)将不再需要 DependencyProperty。

    然后您可以简单地使用停靠面板而不是自定义控件进行绑定。在 Dockpanel 上设置绑定到 IsEnabled 的变量将自动启用或禁用 Dockpanel 中包含的项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 1970-01-01
      • 2021-02-18
      • 2017-11-05
      • 1970-01-01
      相关资源
      最近更新 更多