DependencyProperty 绑定不起作用
我的目标是根据在窗口后面的代码执行期间将更改的标志值来禁用和启用 GUI 上的某些项目。我已经设置了 DependencyProperty 来完成此任务。我相信我所要做的就是将此属性绑定到适当的“IsEnabled”属性,并且一切都应该正常。有些东西没有正确连接,所以什么也没有发生。我在这里遗漏了一些语法吗?
以下是 WPF (MainWindow.xaml) 中的绑定:
<MenuItem Name="LoggingMenuItem" Header="_Logging" IsCheckable="True" Checked="LoggingMenuItem_Checked" IsEnabled="{Binding ElementName=IsMonitoring}" />
以下是后面代码 (MainWindow.xaml.cs) 中“IsMonitoring”属性的声明:
public static readonly DependencyProperty IsMonitoringProperty =
DependencyProperty.Register("IsMonitoring", typeof(Boolean), typeof(Window));
public bool IsMonitoring
{
get { return (bool)GetValue(IsMonitoringProperty); }
set { SetValue(IsMonitoringProperty, value); }
}
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?
Here is the binding in WPF (MainWindow.xaml):
<MenuItem Name="LoggingMenuItem" Header="_Logging" IsCheckable="True" Checked="LoggingMenuItem_Checked" IsEnabled="{Binding ElementName=IsMonitoring}" />
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); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的绑定配置不正确。 ElementName 属性应指向窗口内的“元素”(即控件),并且应使用 Path 属性来指定属性名称。
就您而言,您想为窗口指定一个要使用的名称。我倾向于使用“this”这个名称,但当然它可以是您想要的任何名称。
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.
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.