开发者

set button datacontext to multiple elements

开发者 https://www.devze.com 2022-12-22 18:43 出处:网络
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 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?


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号