简体   繁体   中英

Set SelectedItem as ToolTip in WPF Combo Box Style in UIResources

I had defined a ComboBox Style in UIResources and using it on all controls in my application as below:

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

Now i am having a requirement to show selected item as tool-tip also. Can I set tool-tip in this style so when user had selected any item it is assigned to tool-tip.Is there any possibility i can set tool-tip in UIResource style or i have to do it in every single combo box?

Thanks in Advance

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

<Setter Property="ToolTip" Value="{Binding SelectedItem, RelativeSource={RelativeSource Self}}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM