Balttazarr
Balttazarr

Reputation: 75

Displaying a tooltip for each Combobox item when the list is opened C# WPF XAML MVVM

I'm developing an application in C# MVVM My question is about adding a tooltip for each item that the ComboBox is binded to. Since there are two items only I want it to show the tooltip whenever I open the dropdown and hover my mouse over one of the items , like this:

if I hover my mouse over the first element in the dropDown I'll get a ToolTip with " first item".. and "second item" when I hover over the second element.

ComboBox is placed in DataGridTemplateColumn -> Cell Template -> DataTemplate

<DataGridTemplateColumn Header="PRĄD POJEMNOŚCIOWY [A]" HeaderStyle="{StaticResource PRAD_POJEMNOSCIOWY}">
 <DataGridTemplateColumn.CellTemplate>
  <DataTemplate>
   <ComboBox Name="PradPojemnosciowyComboBox"
             SelectedValue="{Binding SelectedItem, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
             ItemsSource="{Binding Path=LiniaWyComboBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             IsEditable="True"
             IsReadOnly="False"
             Text="{Binding Prad_pojemnosciowy, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             IsTextSearchEnabled="False" 
             IsSynchronizedWithCurrentItem="True"
             PreviewKeyDown="PradPojemnosciowyComboBox_OnPreviewKeyDown">

          <ComboBox.Style>
           <Style TargetType="ComboBox">
            <Style.Triggers>
            <Trigger Property="SelectedValue" Value="{x:Null}">
          <Setter Property="SelectedIndex" Value="{Binding LiniaWyComboBox}"/>
            </Trigger>
           </Style.Triggers>
          </Style>
    </ComboBox.Style>
   </ComboBox>
  </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

UPDATE

ToolTipLabel.cs:

using System.ComponentModel;
using System.Collections.ObjectModel;

namespace GPZmodel.UserControlsGraphicGenerators
{
   public class ToolTipLabel : INotifyPropertyChanged
{
    private string _toolTipText;
    public string ToolTipText
    {
        get { return _toolTipText;}
        set
        {
            if (_toolTipText != value)
            {
                _toolTipText = value;
            }
        }
    }
    public ObservableCollection<ToolTipLabel> ToolTipList = new ObservableCollection<ToolTipLabel>()
    {
        new ToolTipLabel() {ToolTipText = "Nazwa1"} ,
        new ToolTipLabel() {ToolTipText = "Nazwa2"} ,

    };

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

}

Upvotes: 1

Views: 4902

Answers (1)

mm8
mm8

Reputation: 169270

You could use an ItemContainerStyle:

<ComboBox Name="PradPojemnosciowyComboBox"
             SelectedValue="{Binding SelectedItem, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
             ItemsSource="{Binding Path=LiniaWyComboBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             IsEditable="True"
             IsReadOnly="False"
             Text="{Binding Prad_pojemnosciowy, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             IsTextSearchEnabled="False" 
             IsSynchronizedWithCurrentItem="True"
             PreviewKeyDown="PradPojemnosciowyComboBox_OnPreviewKeyDown">
    <ComboBox.Style>
        <Style TargetType="ComboBox">
            <Style.Triggers>
                <Trigger Property="SelectedValue" Value="{x:Null}">
                    <Setter Property="SelectedIndex" Value="{Binding LiniaWyComboBox}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <TextBlock Text="{Binding}" />
                </Setter.Value>
            </Setter>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

Bind the TextBlock in the ToolTip to whatever property of your data object that you want to display.

Upvotes: 4

Related Questions