9

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>
wonea
  • 3,987
  • 17
  • 71
  • 134
Relativity
  • 6,210
  • 20
  • 74
  • 121

2 Answers2

6

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>
sa_ddam213
  • 39,994
  • 7
  • 93
  • 106
  • I tried this...but not working... If I put the following setter as default ..it guives me the correct values DAW and generic when ever i select them...but inside the it is not working. – Relativity Mar 05 '13 at 23:57
  • Do you have `SelectedvaluePath` set on your `ComboBox` – sa_ddam213 Mar 06 '13 at 00:11
  • What data type is in the combobox, can you post your ComboBox xaml, and a example of your collection. – sa_ddam213 Mar 06 '13 at 00:16
  • Thanks for your help...I have figured it out ...instead of using data triggger with selected value..i used data trigger with original model object...now it is working fine. Thanks – Relativity Mar 06 '13 at 16:29
2

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}"/>
wonea
  • 3,987
  • 17
  • 71
  • 134
Syed Waqas
  • 862
  • 1
  • 9
  • 28
  • This will work for a single pass, but it won't update the ToolTip with subsequent viewmodel changes. See https://stackoverflow.com/a/7313801/271200 – wonea Mar 28 '18 at 14:38