Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the ToolTip of WPF ComboBox based on selected value?

I have a ComboBox in my WPF application. Using below code I can set the ToolTip as selected value:

ToolTip="{Binding Path=SelectedValue, RelativeSource={RelativeSource Self}}" 

But if I need to set a separate value for ToolTip based on ComboBox selection, the following code is not working:

<controls:ComboBoxEx.Style>
    <Style TargetType="ComboBox" BasedOn="{StaticResource basicStyle}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=SelectedValue, RelativeSource={RelativeSource Self}}" Value="DAW">
                <Setter Property="ToolTip" Value="abc"/>
            </DataTrigger>

            <DataTrigger Binding="{Binding Path=SelectedValue, RelativeSource={RelativeSource Self}}" Value="generic">
                <Setter Property="ToolTip" Value="def"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</controls:ComboBoxEx.Style>
like image 419
Relativity Avatar asked Mar 05 '13 23:03

Relativity


2 Answers

I'm not sure if I understand correctly, but if you are using a Style you should not have to use a DataTrigger or RelativeSource={RelativeSource Self}}" to access SelectedValue, you should be able to access via a Trigger using the Property

<Style TargetType="ComboBox">
    <Style.Triggers>
        <Trigger Property="SelectedValue"  Value="DAW">
            <Setter Property="ToolTip" Value="abc"/>
        </Trigger>
        <Trigger Property="SelectedValue" Value="generic">
            <Setter Property="ToolTip" Value="def"/>
        </Trigger>
    </Style.Triggers>
</Style>
like image 154
sa_ddam213 Avatar answered Oct 27 '22 09:10

sa_ddam213


Bind the tooltip to the display property of the selected item in this case i have property name display, if you have declarative ComboBox items then that would be

ToolTip="{Binding Path=SelectedItem.Content,ElementName=cmbbox_years}"

Else for custom object below code will work

<ComboBox 
  Name="cmbbox_years" 
  DisplayMemberPath="display" 
  SelectedValuePath="value"
  ItemsSource="{Binding Years}" 
  SelectedItem="{Binding YearSelectedItem}" 
  ToolTip="{Binding Path=SelectedItem.display,ElementName=cmbbox_years}"/>
like image 32
Syed Waqas Avatar answered Oct 27 '22 07:10

Syed Waqas