DependencyProperty绑定不起作用 [英] DependencyProperty binding not working

查看:734
本文介绍了DependencyProperty绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是根据在窗口代码中执行期间将被更改的标志的值来禁用和启用GUI上的某些项目。我设置了一个DependencyProperty来完成这个。我相信我所要做的是把这个属性绑定到适当的IsEnabled属性,一切都应该起作用。有些东西没有正确挂钩,所以没有任何事情发生。我在这里或某些东西缺少一些语法?

My objective here is to disable and enabled some items on a GUI based on the value of a flag that will be changed during execution in the code behind for the window. I've set up a DependencyProperty to accomplish this. I believe that all I have to do is bind the this property to the appropriate "IsEnabled" properties and everything should work. Something is not hooking up properly so nothing is happening. Am I missing some syntax here or something?

这是WPF(MainWindow.xaml)中的绑定:

Here is the binding in WPF (MainWindow.xaml):

<MenuItem Name="LoggingMenuItem" Header="_Logging" IsCheckable="True" Checked="LoggingMenuItem_Checked" IsEnabled="{Binding  ElementName=IsMonitoring}" />

这里是代码背后的IsMonitoring属性的声明(MainWindow.xaml.cs) :

Here is the declaration of the "IsMonitoring" Property in the code behind (MainWindow.xaml.cs):

public static readonly DependencyProperty IsMonitoringProperty = 
   DependencyProperty.Register("IsMonitoring", typeof(Boolean), typeof(Window));

public bool IsMonitoring
{
   get { return (bool)GetValue(IsMonitoringProperty); }
   set { SetValue(IsMonitoringProperty, value); }
}


推荐答案

您的绑定未配置正确的ElementName属性应指向Window中的Element(即控件),您应该使用Path属性来指定属性名称。

Your binding is not configured correctly. The ElementName attribute should point to an "Element" (that is control) within the Window and you should use the Path attribute to specify the property name.

在您的情况下,你想给Window一个使用的名字。我倾向于使用这个名称,但当然可以是你想要的。

In your case, you want to give the Window a name to use. I tend to use the name "this" but of course it could be whatever you want.

<Window x:Name="this"
        ...
        >
    ...
    <MenuItem IsEnabled="{Binding ElementName=this, Path=IsMonitoring}" />
    ...
</Window>

这篇关于DependencyProperty绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆