1

I'm stuck with a seemingly simple thing in WPF. And searching didn't help so far.

            <Viewbox IsEnabled="{Binding IsEnabled, ElementName = Button_A}">
                <Button x:Name="Button_B" Click="Button_B_Click" ></Button>
            </Viewbox>

Now the above XAML let's the Button_B to be enabled when the Button_A is enabled and the Button_B to be disabled when the Button_A is disabled.

But I want the Button_B to be disabled when the Button_A is enabled; and Button_B to be enabled when the Button_A is disabled as one way binding(A dictates B).

How can this be achieved using only XAML?

4
  • Why would wrap a Button into a ViewBox? You should avoid doing this. Commented Jul 22, 2021 at 14:52
  • I was doing that to make buttons and labels resizable. Why should it be avoided? Thanks Commented Jul 22, 2021 at 16:47
  • This is some unessesary overhead that you introduce here by wrapping each control into a Viewbox. A Viewbox forces the complete child tree to render again in order to scale its content. WPF provides different layout mechanisms like HorizontalAlignment to enable dynamic or responsive layout. There are different containers that wil allow child elements to grow. I wonder how many professional applications you have used where buttons grow and shrink when you resize the form. Same applies to text elements like labels. Commented Jul 22, 2021 at 20:47
  • If content is too large for the view port you usually use scroll viewers. If available area grows too big you can use the additional space to reveal more content e.g. show more items of a list view or improve the UX by rearranging the layout. Commented Jul 22, 2021 at 20:47

1 Answer 1

2

Simply use a Style (Styling and Templating an ItemsControl) and add a DataTrigger to it:

<Button x:Name="Button_A" />

<Button x:Name="Button_B">
  <Button.Style>
    <Style TargetType="FrameworkElement">
      <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=Button_A, Path=IsEnabled}" Value="True">
          <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>
Sign up to request clarification or add additional context in comments.

2 Comments

But where is Button_B mentioned in your XAML?
The button without name? the name doesn't matter. It's a button that depends on a state of another button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.