【问题标题】:Displaying a tooltip for each Combobox item when the list is opened C# WPF XAML MVVM打开列表时为每个 Combobox 项显示工具提示 C# WPF XAML MVVM
【发布时间】:2019-06-05 05:29:27
【问题描述】:

我正在用 C# MVVM 开发一个应用程序 我的问题是为 ComboBox 绑定到的每个项目添加一个工具提示。由于只有两个项目,我希望它在打开下拉列表并将鼠标悬停在其中一个项目上时显示工具提示,如下所示:

如果我将鼠标悬停在下拉菜单中的第一个元素上,当我将鼠标悬停在第二个元素上时,我会得到一个带有“第一项”和“第二项”的工具提示。

ComboBox放在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>

更新

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

}

【问题讨论】:

    标签: c# wpf mvvm combobox tooltip


    【解决方案1】:

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

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

    【讨论】:

    • 嗨!当我将鼠标移到项目上时,它确实显示了一个小“框”,但我的 ComboBox 样式更改为......看起来更糟。它也不与我的 ToolTipLabel 绑定。见更新。我知道我应该添加一些逻辑,但类似于 if(ComboBoxItem.index == 0) ToolTipText = ToolTipList[0];否则 {...}
    • 您的DataGrid 是否绑定到ToolTipList?或者什么是 LiniaWyComboBox?
    • 不,不是。它绑定到 LineOut 类的 LiniaWyComboBox 属性,这是一个 ObservableCollection。 "LineWy" = "LineOut"
    • 如果您在 DataGrid 中显示双精度值,您希望在工具提示中显示什么?
    • 我希望有人知道组合框中的这两个值是什么意思,只是关于这些值的信息。你的意思是当 DataGrid 绑定到双打时,Combobox 不会更新其他任何东西?就像我解释的那样?感谢您帮助我:)
    猜你喜欢
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多