自定义用户控件“IsEnabled”数据绑定不起作用
我的 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");
}
}
当我尝试启动我的应用程序时,用户控件正在启动...已启用。 我到处都添加了断点,EditorEnabled 从一开始就是 false。
我还做了一件非常愚蠢的事情来试图弄清楚发生了什么:我创建了一个转换器(非常有用 - 将布尔值转换为布尔值 - 呃),在其上放置一个断点,然后......代码从未到达。
<my:UcComponentEditor Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
IsEnabled="{Binding EditorEnabled, Converter={StaticResource BoolConverter}}"
DataContext="{Binding VmComponent}" />
这可能意味着属性 isEnabled 永远不会被设置,因为永远不会到达转换器。
您看到那里有什么问题吗?我大约一周前开始在 WPF 中工作,因此我可能错过了一些重要的东西...
非常感谢您的宝贵时间:-)
I have a kinda awful problem with my WPF application right now...
I have a custom UserControl used to edit details of a component. It should start by being not enabled, and become enabled as soon as the user chose a component to edit.
The problem is: the IsEnabled property does not even change.
Here is my code:
<my:UcComponentEditor Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
IsEnabled="{Binding EditorEnabled}"
DataContext="{Binding VmComponent}" />
EditorEnabled is a property in my ViewModel (VmComponent), and is by default false, becomes true when the user chose a component or created one
Just for the record, in my ViewModel:
private Boolean _editorEnabled = false;
public Boolean EditorEnabled
{
get { return _editorEnabled; }
set
{
_editorEnabled = value;
OnPropertyChanged("EditorEnabled");
}
}
When I try to launch my app, the UserControl is starting... enabled.
I added breakpoints everywhere, the EditorEnabled is false from the beginning.
I also did a horribly stupid thing to try to figure out what's happening: I created a converter (so useful -- converting a boolean to boolean -- eh), put a breakpoint on it, and... The code is never reached.
<my:UcComponentEditor Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
IsEnabled="{Binding EditorEnabled, Converter={StaticResource BoolConverter}}"
DataContext="{Binding VmComponent}" />
That probably means that the property isEnabled is never set, since the converter is never reached.
Do you see any kind of problem there? I started working in WPF about one week ago and therefore I may have missed something essential...
Thank you very much for your time :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该添加 DependencyProperty 以使绑定正常工作。 请参阅此处了解更多信息。
代码隐藏:
You should add a DependencyProperty for the binding to work properly. See here for more information.
Code-behind:
我认为问题是用户控件的 DataContext 属性上存在绑定。这意味着 EditorEnabled 属性应该是 VmComponent 对象中的属性。至少这就是我的问题所在。
为了解决这个问题,我为 IsEnabled 的绑定指定了正确的源。一旦我这样做了,控件就开始按预期工作。
希望有帮助。
The issue I think is that there is a binding on the DataContext property of the user control. Which means the EditorEnabled property should be a property in the VmComponent object. At least that's what my problem was.
To get around it, I specified a proper source to the binding of IsEnabled. Once I did that the control started working as expected.
Hope that helps.
将控件封装在 DockPanel 中(例如)将消除对 DependencyProperty 的需要。
然后,您可以简单地使用停靠面板而不是自定义控件进行绑定。在 Dockpanel 上设置绑定到 IsEnabled 的变量将自动启用或禁用 Dockpanel 中包含的项目。
Encapsulating your control in a DockPanel (for example) will remove the need for a DependencyProperty.
You can then simply do your binding with the dockpanel instead of the custom control. Setting the variable bound to IsEnabled on the Dockpanel will automatically enable or disable the items contained in the Dockpanel.