简体   繁体   中英

WPF ToolTip Visibility Binding to RadioButton Checked

I want to show/hide tooltips on radio buttons based on which radio button in a different group is checked. I tried the BooleanToVisibilityConverter and it works for other Controls, but not ToolTips.

<Page.Resources>
    <BooleanToVisibilityConverter x:Key="b2v" />
</Page.Resources>

Radio buttons to determine if the tooltip is visible. ToolTip should be visible when "InputText" is checked.

<RadioButton x:Name="InputText" Content="InputText" IsChecked="True" />
<RadioButton x:Name="Other" Content="Other" />

Radio button with tooltip

<RadioButton x:Name="InputRows" Content="Rows">
    <RadioButton.ToolTip>
        <ToolTip x:Name="InputRowsToolTip"
                 Visibility="{Binding IsChecked,ElementName=InputText, Converter={StaticResource b2v}}">
            ToolTip text here
        </ToolTip>
    </RadioButton.ToolTip>
</RadioButton>

Other things I've tried that don't work:

  • Wrap the tooltip text in a <TextBlock> and include the Visibility parameter there - no effect.

  • Use <TextBlock> instead of <ToolTip> and include the Visibility parameter there - no effect.

  • Add ToolTipService.ShowOnDisabled="False" and ToolTipService.IsEnabled="False" to ToolTip and RadioButton - tooltip does not show at all.

I can get it to behave the way I want with methods in the cs file for InputTextRadioButton_Checked and OtherRadioButton_Checked and set the InputRowsToolTip.Visibility = Visibility.Visible or Visibility.Collapsed depending on RadioButton, but it seems like I should be able to do this with the BooleanToVisibilityConverter since it works for other Controls.

I Just added DataContext in InputRows

DataContext="{Binding ElementName=InputText"

And Your code.

<RadioButton x:Name="InputText" Content="InputText" IsChecked="True" />
<RadioButton x:Name="Other" Content="Other" />

<RadioButton x:Name="InputRows" Content="Rows" DataContext="{Binding ElementName=InputText}">
    <RadioButton.ToolTip>
        <ToolTip x:Name="InputRowsToolTip"
                 Visibility="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.IsChecked, Converter={StaticResource b2v}}">
            ToolTip text here
        </ToolTip>
    </RadioButton.ToolTip>
</RadioButton>

The control inside the tooltip area of the radio button does not directly find the control outside. That's why I think it's better to deal with it through DataContext, a powerful Dependency Property.

You cannot use the form below.

  • ElementName
  • RelativeSource AncestorType
  • Relative Source TemplateParent

this sample sourcecode https://github.com/ncoresoftsource/stackoverflowsample/tree/main/src/answers/radiobutton-tooltip-other-binding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM