将 IsEnabled 绑定到父 ViewModel 而不是 UserControl ViewModel

发布于 2024-10-24 04:09:15 字数 705 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

擦肩而过的背影 2024-10-31 04:09:15

我不会以某种方式修改绑定(例如,您可以使其依赖于其他答案中建议的其他控件名称),我会单独移动将被禁用的控件和 DataContext 所在的控件被改变。例如:

<ContentControl IsEnabled="{Binding CanModify}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
    <localControls:TeamEmployeeSelector DataContext="{Binding Confidentiality}"/>
</ContentControl>

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:

<ContentControl IsEnabled="{Binding CanModify}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
    <localControls:TeamEmployeeSelector DataContext="{Binding Confidentiality}"/>
</ContentControl>
帥小哥 2024-10-31 04:09:15

我将这样做。

您的 TeamEmployeeSelector UserControl 将包含一个根级别元素,默认情况下该元素是 Grid 并命名为“LayoutRoot”。

现在,您可以将所有子元素的 IsEnabled 属性绑定到 UserControl,如下所示:-

 <TextBox IsEnabled="{Binding Parent.IsEnabled, ElementName=LayoutRoot}" ... />

通过使用元素到元素绑定,您无需复制 >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 a Grid and is given the name "LayoutRoot".

Now you can bind the IsEnabled property of all the child elements to the UserControl like this:-

 <TextBox IsEnabled="{Binding Parent.IsEnabled, ElementName=LayoutRoot}" ... />

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 the Parent 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.

不疑不惑不回忆 2024-10-31 04:09:15

这是一个范围界定问题。通常,在创建 UserControl 时,您希望将其自身设置为其子元素的 DataContext。这在构造函数中最容易完成:

UserControlExample() {
   InitializeComponent();
   RootElement.DataContext = this;
}

其中 RootElement 是您为 UserControl 的第一个子项(通常是网格或面板)指定的名称。

从这里,您可以为子元素设置自然绑定,如下所示:

<TextBox x:Name="MainTextBox" IsEnabled={Binding IsEnabled} />

这有效,因为 TextBox 继承了父布局面板的 DataContext

最后,如果您想让 UserControlIsEnabled 属性与其父级相关,最好在声明时完成:

<Grid>
   <UserControlExample IsEnabled={Binding CanModify} />
</Grid>

这样您就可以保留关注点分离。子控件不关心 UserControl 所反映的内容。他们只需要知道当控件的 IsEnabled 属性翻转时如何启用/禁用。

sub-controls IsEnabled bound to --> (UserControlExample is DataContext)
   UserControlExample.IsEnabled bound to -->  (VM is DataContext)
      VM.CanModify  

This is a scoping issue. Generally, when creating a UserControl, you want to set itself as the DataContext for its sub-elements. This is most easily accomplished in the constructor:

UserControlExample() {
   InitializeComponent();
   RootElement.DataContext = this;
}

Where RootElement is the name you give to this first child (usually a Grid or panel) of your UserControl.

From here you can set natural bindings for your sub-elements like so:

<TextBox x:Name="MainTextBox" IsEnabled={Binding IsEnabled} />

This works, since TextBox inherits the DataContext of the parent layout panel.

Finally, if you want to have your UserControl's IsEnabled property to be related to its parent, this is best done at the point of declaration:

<Grid>
   <UserControlExample IsEnabled={Binding CanModify} />
</Grid>

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's IsEnabled property flips.

sub-controls IsEnabled bound to --> (UserControlExample is DataContext)
   UserControlExample.IsEnabled bound to -->  (VM is DataContext)
      VM.CanModify  
半暖夏伤 2024-10-31 04:09:15

我不知道在 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 !

怕倦 2024-10-31 04:09:15
<localControls:TeamEmployeeSelector Grid.Row="1" Grid.Column="0"
Grid.ColumnSpan="2" IsEnabled="{Binding ElementName=SomeElementName_With_Parent_ViewModel, Path=DataContext.CanModify}" DataContext="{Binding Confidentiality}"/>
<localControls:TeamEmployeeSelector Grid.Row="1" Grid.Column="0"
Grid.ColumnSpan="2" IsEnabled="{Binding ElementName=SomeElementName_With_Parent_ViewModel, Path=DataContext.CanModify}" DataContext="{Binding Confidentiality}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文