在UIResources中的WPF组合框样式中将SelectedItem设置为ToolTip

时间:2018-06-25 10:22:17

标签: c# .net wpf

我已经在UIResources中定义了一个ComboBox样式,并在我的应用程序中的所有控件上使用了它,如下所示:

<Style x:Name="combo" TargetType="{x:Type ComboBox}">
        <Setter Property="UIElement.SnapsToDevicePixels" Value="True"/>
        <Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
        <Setter Property="TextElement.Foreground" Value="White"/>
        <Setter Property="FrameworkElement.FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton
                                ClickMode="Press"
                                Name="ToggleButton"
                                IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                Focusable="False"
                                Grid.Column="2"
                                Template="{StaticResource ComboBoxToggleButtonTemplate}"/>
                        <ContentPresenter
                                Margin="3,3,23,3"
                                HorizontalAlignment="Left"
                                Name="ContentSite"
                                VerticalAlignment="Center"
                                ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"
                                Content="{TemplateBinding ComboBox.SelectionBoxItem}"
                                IsHitTestVisible="False" />
                        <TextBox
                                Margin="3,3,23,3"
                                Visibility="Hidden"
                                HorizontalAlignment="Left"
                                Name="PART_EditableTextBox"
                                Background="Transparent"
                                VerticalAlignment="Center"
                                Style="{x:Null}"
                                IsReadOnly="False"
                                Focusable="True"
                                xml:space="preserve"
                                Template="{StaticResource ComboBoxTextBoxTemplate}"/>
                        <Popup
                                Placement="Bottom"
                                Name="Popup"
                                Focusable="False"
                                AllowsTransparency="True"
                                IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}"
                                PopupAnimation="Fade" >
                            <Grid
                                    MinWidth="{TemplateBinding FrameworkElement.ActualWidth}"
                                    MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}"
                                    Name="DropDown"
                                    SnapsToDevicePixels="True">
                                <Border
                                        BorderBrush="#FF404040"
                                        BorderThickness="1,1,1,1"
                                        Name="DropDownBorder"
                                        Background="#FF404040" />
                                <ScrollViewer
                                        Margin="4,6,4,6"
                                        SnapsToDevicePixels="True">
                                    <ItemsPresenter
                                            KeyboardNavigation.DirectionalNavigation="Contained"/>
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                           <Border x:Name="border" 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                SnapsToDevicePixels="True"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ItemsControl.HasItems" Value="False">
                            <Setter Property="FrameworkElement.MinHeight" TargetName="DropDownBorder" Value="95"/>
                        </Trigger>
                        <Trigger Property="UIElement.IsEnabled" Value="False">
                            <Setter Property="TextElement.Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                        <Trigger Property="ItemsControl.IsGrouping" Value="True">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
                        </Trigger>
                        <Trigger Property="Window.AllowsTransparency" SourceName="Popup" Value="True">
                            <Setter Property="FrameworkElement.Margin" TargetName="DropDownBorder" Value="0,0,0,0"/>
                        </Trigger>
                        <Trigger Property="ComboBox.IsEditable" Value="True">
                            <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                            <Setter Property="UIElement.Visibility" TargetName="PART_EditableTextBox" Value="Visible"/>
                            <Setter Property="UIElement.Visibility" TargetName="ContentSite" Value="Hidden"/>
                        </Trigger>
                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter Property="Opacity" TargetName="border" Value="1"/>
                            <Setter Property="BorderBrush" Value="DodgerBlue"/>
                            <Setter Property="BorderThickness" Value="1"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

现在我有一个要求也将所选项目显示为工具提示。我可以以这种样式设置工具提示吗,以便当用户选择分配给工具提示的任何项目时,是否有可能我可以以UIResource样式设置工具提示,还是必须在每个组合框中都进行设置? / p>

预先感谢

1 个答案:

答案 0 :(得分:2)

根据要绑定的项目类型,定义的方式以及要在工具提示中显示的内容,您可以在绑定{{1的<Setter />的{​​{1}}中添加Style }}属性改为ToolTip属性:

SelectedItem