将 IsEnabled 绑定到父 ViewModel 而不是 UserControl ViewModel
我在 SilverLight 中开发了一个用户控件,其中包含多个子控件。 文本框
、组合框
等等。
问题是,当我将该 UserControl
包含到父视图中并将完整控件设置为 IsEnabled=False
时,该特定 UserControl
中的子控件> 仍然启用。
毕竟我发现了问题。
添加类似的内容意味着 IsEnabled
绑定位于 UserControl
绑定中,而不是像我在父级的 DataContext
中所期望的那样。
<localControls:TeamEmployeeSelector Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
IsEnabled="{Binding CanModify}" DataContext="{Binding Confidentiality}"/>
问题:
但仍然存在一个问题,如何将 IsEnabled
绑定到 Parent 的 ViewModel ?因为将 CanModify
属性复制到子控件的 ViewModel 中并不是很优雅。
I developed a user control in SilverLight that contains several child controls. Textboxes
, ComboBoxes
and so on.
The problem is, when I include that UserControl
into a parent view and set the complete control to IsEnabled=False
, the child controls in that specific UserControl
are still enabled.
After all I found the problem.
Adding something like that, implies that the IsEnabled
Binding is located in the UserControl
binding, not as expected from myself in the DataContext
of the parent.
<localControls:TeamEmployeeSelector Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
IsEnabled="{Binding CanModify}" DataContext="{Binding Confidentiality}"/>
QUESTION:
But there's still the question how I can bind the IsEnabled
to the ViewModel of the Parent? Because it's not very elegant to copy the CanModify
Property to the ViewModel of the Child Control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不会以某种方式修改绑定(例如,您可以使其依赖于其他答案中建议的其他控件名称),我会单独移动将被禁用的控件和
DataContext
所在的控件被改变。例如:Instead of modifying a binding in some way (for example you can make it dependent on other control name as it is proposed in other answer) I would move separate the control which will be disabled and control where
DataContext
will be changed. For example:我将这样做。
您的
TeamEmployeeSelector
UserControl
将包含一个根级别元素,默认情况下该元素是Grid
并命名为“LayoutRoot”。现在,您可以将所有子元素的
IsEnabled
属性绑定到UserControl
,如下所示:-通过使用元素到元素绑定,您无需复制
>CanModify
属性添加到子视图模型中。有些人可能建议您只需将
x:Name
添加到 UserControl 元素,然后直接绑定到它,而不是像我上面那样通过根元素的Parent
属性。这在 Silverlight 4 中可以正常工作,但在 3 或 WP7 中则不行。我个人更喜欢上面的。Here is how I would do this.
Your
TeamEmployeeSelector
UserControl
will contain a single root level element which by default is aGrid
and is given the name "LayoutRoot".Now you can bind the
IsEnabled
property of all the child elements to theUserControl
like this:-By using element-to-element binding you do not need to copy the
CanModify
property in to child view models.Some might suggest that you simply add an
x:Name
to your UserControl element and then bind directly to it rather than going via theParent
property of the root element as I do above. That'll work ok in Silverlight 4 but not in 3 or WP7. Personally I prefer the above.这是一个范围界定问题。通常,在创建
UserControl
时,您希望将其自身设置为其子元素的DataContext
。这在构造函数中最容易完成:其中
RootElement
是您为UserControl
的第一个子项(通常是网格或面板)指定的名称。从这里,您可以为子元素设置自然绑定,如下所示:
这有效,因为
TextBox
继承了父布局面板的DataContext
。最后,如果您想让
UserControl
的IsEnabled
属性与其父级相关,最好在声明时完成:这样您就可以保留关注点分离。子控件不关心
UserControl
所反映的内容。他们只需要知道当控件的 IsEnabled 属性翻转时如何启用/禁用。This is a scoping issue. Generally, when creating a
UserControl
, you want to set itself as theDataContext
for its sub-elements. This is most easily accomplished in the constructor:Where
RootElement
is the name you give to this first child (usually a Grid or panel) of yourUserControl
.From here you can set natural bindings for your sub-elements like so:
This works, since
TextBox
inherits theDataContext
of the parent layout panel.Finally, if you want to have your
UserControl
'sIsEnabled
property to be related to its parent, this is best done at the point of declaration:This way you keep your concerns separate. The sub-controls don't care what the
UserControl
is reflecting. They just need to know how to enable/disable when the control'sIsEnabled
property flips.我不知道在 Silverlight 中是否可行,但在 WPF 中我会使用relativesource。
请查看此处。
希望这有帮助!
I don't know if it's possible in Silverlight, but in WPF I would use RelativeSource.
Have a look here.
Hope this help !