开发者

WPF binding two buttons together

开发者 https://www.devze.com 2023-02-20 14:14 出处:网络
I work in Windows Presentation Foundation and I need to bind two buttons together. The first button is a classic button, the second is a toggle button. When the user presses the toggle button it becom

I work in Windows Presentation Foundation and I need to bind two buttons together. The first button is a classic button, the second is a toggle button. When the user presses the toggle button it becomes "ON" and in this very moment I need to disable开发者_如何学C the classic button. When the user presses the toggle button again and it becomes "OFF", the classic button should become "enabled". Is there any way to do this without a classic binding with converter. I mean triggers or something like that.


You can do it with triggers, but it'd be much more concise with a converter. A triggers version would be something like this:

<Button Content="One">
  <Button.Style>
    <Style TargetType="Button">
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsChecked, ElementName=tb}" Value="True">
          <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>
<ToggleButton x:Name="tb">Two</ToggleButton>

With a suitable converter declared as a resource it'd be more like this:

<Button Content="One" IsEnabled="{Binding IsChecked, ElementName=tb, Converter={StaticResource BooleanInverterConverter}}" />
<ToggleButton x:Name="tb">Two</ToggleButton>
0

精彩评论

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