none
ComboboxItem的ToolTip显示位置太乱 RRS feed

  • 问题

  •         <ComboBox.ItemTemplate>
                <DataTemplate>
                      <DockPanel LastChildFill="True">
                             <DockPanel.ToolTip>
                                    <ToolTip Placement="Right">
                                        <TextBlock  Text="{Binding Details"/>
                                    </ToolTip>
                                </DockPanel.ToolTip>
                                <TextBlock DockPanel.Dock="Top" Text="{Binding Name}""/>
                                <TextBlock  Text="{Binding ShortDescription}"/>
                            </DockPanel>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>


    设置了ComboBoxItem的ToolTip属性,但是由于Name和Description长短不一,导致鼠标一定要放到有文字的地方才会显示ToolTip,且ToolTip的位置一直在变。

    请问:

    1.如何让鼠标划过ComboboxItem(包括空白区域)的时候就显示Tooltip?

    2.如何固定ToolTip的显示位置?(水平位置)

    2018年3月22日 6:52

答案

  • Hi ShannonZhou,

    你可以这样做:

    <ComboBox
                Width="200"
                Height="30"
                Margin="5"
                ItemsSource="{Binding}">
                <ComboBox.ItemContainerStyle>
                    <Style>
                        <Setter Property="Control.ToolTip" Value="{Binding Details}"/>
                    </Style>
                </ComboBox.ItemContainerStyle>
                <ComboBox.ItemTemplate>
                    
                    <DataTemplate>
                        <DockPanel LastChildFill="True">
                            <!--<DockPanel.ToolTip>
                                <ToolTip Placement="Right">
                                    <TextBlock Text="{Binding Details}" />
                                </ToolTip>
                            </DockPanel.ToolTip>-->
                            <TextBlock DockPanel.Dock="Top" Text="{Binding Name}" />
                            <TextBlock Text="{Binding ShortDescription}" />
                        </DockPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

    以下的代码是改变ToolTip的位置, 你可以看一下:

    https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/how-to-position-a-tooltip

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年3月23日 9:59
    版主

全部回复

  • Hi ShannonZhou,

    你可以这样做:

    <ComboBox
                Width="200"
                Height="30"
                Margin="5"
                ItemsSource="{Binding}">
                <ComboBox.ItemContainerStyle>
                    <Style>
                        <Setter Property="Control.ToolTip" Value="{Binding Details}"/>
                    </Style>
                </ComboBox.ItemContainerStyle>
                <ComboBox.ItemTemplate>
                    
                    <DataTemplate>
                        <DockPanel LastChildFill="True">
                            <!--<DockPanel.ToolTip>
                                <ToolTip Placement="Right">
                                    <TextBlock Text="{Binding Details}" />
                                </ToolTip>
                            </DockPanel.ToolTip>-->
                            <TextBlock DockPanel.Dock="Top" Text="{Binding Name}" />
                            <TextBlock Text="{Binding ShortDescription}" />
                        </DockPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

    以下的代码是改变ToolTip的位置, 你可以看一下:

    https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/how-to-position-a-tooltip

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年3月23日 9:59
    版主
  • 完美解决,非常感谢!!!
    2018年3月26日 1:32