将按钮数据上下文设置为多个元素

发布于 2024-08-24 20:55:15 字数 246 浏览 4 评论 0原文

我有一个按钮,当窗口中发生验证错误时需要禁用该按钮。可能发生这些错误的项目都是文本框。

我已经这样绑定了按钮的数据上下文:

DataContext="{Binding ElementName=txtEmail}"

现在有了这个,当电子邮件文本框中发生验证错误时,我可以将按钮样式设置为禁用,但我也想在窗口中的其他文本框中发生验证错误时也这样做吗?

如何将此绑定设置为多个文本框?

I've got a button that I need to be disabled when validation errors occur in my window. The items on which these errors can occur are all textboxes.

I've bound my Button's datacontext as such:

DataContext="{Binding ElementName=txtEmail}"

Now with this, I can set the button style to disabled when validation errors occur in the email textbox, but I want to do it also when it occurs in other textboxes in my window?

How can I set this binding to multiple textboxes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

私野 2024-08-31 20:55:15

你不能,至少不能直接。您可以使用 MultiBinding 将所有所需的文本框作为输入,但您需要提供一个 IMultiValueConverter 将各个文本框“组合”为一个对象(例如列表):

<Button>
  <Button.DataContext>
    <MultiBinding Converter="{StaticResource ListMaker}">
      <Binding ElementName="txtEmail" />
      <Binding ElementName="txtFirstName" />
      <Binding ElementName="txtLastName" />
    </MultiBinding>
  </Button.DataContext>
</Button>

然后生成的列表对象将被传递到您的触发器,因此您将无法直接访问 Validation.HasError 属性:您的 DataTrigger 还需要引入一个转换器,该转换器将列表对象转换为布尔值,指示是否为任何内容设置了 Validation.HasError在列表中。此时,您不妨忘记触发器并使用 MultiBinding 绑定 IsEnabled:(

<Button>
  <Button.IsEnabled>
    <MultiBinding Converter="{StaticResource AllFalse}">
      <Binding Path="(Validation.HasError)" ElementName="txtEmail" />
      <Binding Path="(Validation.HasError)" ElementName="txtFirstName" />
      <Binding Path="(Validation.HasError)" ElementName="txtLastName" />
    </MultiBinding>
  </Button.DataContext>
</Button>

这里,如果所有输入均为 false,则 AllFalse 转换器返回 true,如果任何输入为 true,则返回 false。)

但是,更好的方法可能是将 Button 直接绑定到其他 UI 元素的方法是,让您的数据对象(文本框绑定到的同一对象)公开 IsValid 属性(带有合适的更改通知),并将 Button.IsEnabled 绑定到该属性

<Button IsEnabled="{Binding IsValid}" />

:您将转向 MVVM 风格的解决方案,该解决方案有助于解决可测试性等问题(例如,为 IsValid 属性创建测试很容易;为 Button.IsEnabled 创建测试则困难得多)。

You can't, at least not directly. You could use a MultiBinding with all of the desired text boxes as inputs, but you will need to provide an IMultiValueConverter to "combine" the various text boxes into one object (such as a list):

<Button>
  <Button.DataContext>
    <MultiBinding Converter="{StaticResource ListMaker}">
      <Binding ElementName="txtEmail" />
      <Binding ElementName="txtFirstName" />
      <Binding ElementName="txtLastName" />
    </MultiBinding>
  </Button.DataContext>
</Button>

And it is then that resulting list object that will be passed to your trigger, so you won't be able to access the Validation.HasError property directly: your DataTrigger will also need to bring in a converter which converts the list object into a boolean indicating whether Validation.HasError is set for anything in the list. At this point you might as well just forget about triggers and bind IsEnabled using a MultiBinding:

<Button>
  <Button.IsEnabled>
    <MultiBinding Converter="{StaticResource AllFalse}">
      <Binding Path="(Validation.HasError)" ElementName="txtEmail" />
      <Binding Path="(Validation.HasError)" ElementName="txtFirstName" />
      <Binding Path="(Validation.HasError)" ElementName="txtLastName" />
    </MultiBinding>
  </Button.DataContext>
</Button>

(Here the AllFalse converter returns true if all inputs are false, and false if any input is true.)

A better approach, however, may be, instead of binding the Button directly to other UI elements, have your data object -- the same object that your text boxes are binding to -- expose an IsValid property (with suitable change notifications), and bind your Button.IsEnabled to that:

<Button IsEnabled="{Binding IsValid}" />

This moves you towards a MVVM-style solution which helps with things like testability (e.g. it's easy to create tests for the IsValid property; it's much harder to create tests for Button.IsEnabled).

单身情人 2024-08-31 20:55:15

对于 MVVM 方法,您可以尝试从 ICommand 实现命令路由器。

<Button Command="{Binding Path=Commands.MyButtonCommand}" Style="{StaticResource MyButtonStyle}" ></Button>

其中 Commands 属性是 ViewModel 的一部分。然后,您可以控制该命令实现的功能以及是否启用它。这样测试就容易多了。

For the MVVM approach you could try implementing a command router from ICommand.

<Button Command="{Binding Path=Commands.MyButtonCommand}" Style="{StaticResource MyButtonStyle}" ></Button>

where the Commands property is part of the ViewModel. You then have control over what functionality the command implements as well as whether it is enabled or not. Testing is then a whole lot easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文