打开列表时,为每个组合框项目显示工具提示C#WPF XAML MVVM

时间:2019-01-10 10:52:44

标签: c# wpf mvvm combobox tooltip

我正在用C#MVVM开发应用程序 我的问题是为组合框绑定到的每个项目添加工具提示。由于只有两个项目,因此我希望每当我打开下拉菜单并将鼠标悬停在其中一个项目上时都显示工具提示,例如:

如果将鼠标悬停在dropDown的第一个元素上,当我将鼠标悬停在第二个元素上时,将显示一个带有“第一项” ..和“第二项”的工具提示。

组合框位于DataGridTemplateColumn->单元格模板-> 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>

更新

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;
}

}

1 个答案:

答案 0 :(得分:0)

您可以使用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>

TextBlock中的ToolTip绑定到要显示的数据对象的任何属性。