1

我想在 WPF 中显示一个基于图像的组合框。它适用于一些模板,但我不知道如何摆脱蓝色的鼠标悬停矩形。见浅蓝色框:

在此处输入图像描述

好的,我找到了解决方案,现在问题已解决,请参阅此处的解决方案:

解决方案:

    <ComboBox x:Name="comboBox"
        Width="158"
              Height="44"
              Background="Transparent"
              BorderBrush="#551B2830"
              Foreground="Black">
        <ComboBoxItem TextBlock.TextAlignment="Center">Mousehover me</ComboBoxItem>
        <ComboBoxItem TextBlock.TextAlignment="Center">To see the bad </ComboBoxItem>
        <ComboBoxItem TextBlock.TextAlignment="Center">blue rectangle</ComboBoxItem>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="ComboBoxItem">
                <Setter Property="BorderBrush" Value="Transparent" />
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <!--This Does the Magic-->
                        <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                            <Border x:Name="Bd"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Padding="{TemplateBinding Padding}"
                                    SnapsToDevicePixels="true">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                  SnapsToDevicePixels="True" />
                            </Border>

                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox>

</Grid>
4

2 回答 2

1

好的,找到了怎么做..我可以为没有边框或不可见边框的组合框项目添加模板。我还用解决方案更新了问题。

编辑:在这里也将其标记为已回答..

 <ComboBox x:Name="comboBox"
    Width="158"
          Height="44"
          Background="Transparent"
          BorderBrush="#551B2830"
          Foreground="Black">
    <ComboBoxItem TextBlock.TextAlignment="Center">Mousehover me</ComboBoxItem>
    <ComboBoxItem TextBlock.TextAlignment="Center">To see the bad </ComboBoxItem>
    <ComboBoxItem TextBlock.TextAlignment="Center">blue rectangle</ComboBoxItem>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Template">
                <Setter.Value>
                    <!--This Does the Magic-->
                    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                        <Border x:Name="Bd"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}"
                                SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              SnapsToDevicePixels="True" />
                        </Border>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
于 2016-05-02T09:17:12.937 回答
0

这对你有用:

<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"KeyboardNavigation.DirectionalNavigation="Contained"/>
    </ScrollViewer></Border>
    </Microsoft_Windows_Themes:SystemDropShadowChrome></Popup>
    <Microsoft_Windows_Themes:ListBoxChrome x:Name="Border" Grid.ColumnSpan="2" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" RenderMouseOver="{TemplateBinding IsMouseOver}"/>
    <TextBox x:Name="PART_EditableTextBox" Margin="{TemplateBinding Padding}" Style="{StaticResource ComboBoxEditableTextBox}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}"/>
    <ToggleButton Style="{StaticResource ComboBoxToggleButton}"Grid.Column="1" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
    </Grid>
    <ControlTemplate.Triggers>
    <Trigger Property="IsKeyboardFocusWithin" Value="true">
    <Setter Property="Foreground" Value="Black"/></Trigger>
    <Trigger Property="IsDropDownOpen" Value="true">
    <Setter Property="RenderFocused" TargetName="Border" Value="true"/></Trigger>
    <Trigger Property="HasItems" Value="false">
    <Setter Property="Height" TargetName="DropDownBorder" Value="95"/></Trigger>
    <Trigger Property="IsEnabled" Value="false">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
    <Setter Property="Background" Value="#FFF4F4F4"/></Trigger>
    <Trigger Property="IsGrouping" Value="true">
    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/></Trigger>
    <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
    <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
    <Setter Property="Color" TargetName="Shdw" Value="#71000000"/></Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    <Style x:Key="ComboBoxStyle1" TargetType="{x:Type ComboBox}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="Padding" Value="4,3"/>
    <Setter Property="Template"><Setter.Value>
    <ControlTemplate TargetType="{x:Type ComboBox}">
    <Grid x:Name="MainGrid" SnapsToDevicePixels="true">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
    </Grid.ColumnDefinitions>
    <Popup x:Name="PART_Popup" Margin="1" AllowsTransparency="true" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Grid.ColumnSpan="2">
    <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}" Color="Transparent">
    <Border x:Name="DropDownBorder" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}"BorderThickness="1">
    <ScrollViewer CanContentScroll="true">
    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"KeyboardNavigation.DirectionalNavigation="Contained"/>
    </ScrollViewer></Border>
    </Microsoft_Windows_Themes:SystemDropShadowChrome></Popup>
    <ToggleButton Style="{StaticResource ComboBoxReadonlyToggleButton}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" IsHitTestVisible="false" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"/>
    </Grid>
    <ControlTemplate.Triggers>
    <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
    <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
    <Setter Property="Color" TargetName="Shdw" Value="#71000000"/></Trigger>
    <Trigger Property="HasItems" Value="false">
    <Setter Property="Height" TargetName="DropDownBorder" Value="95"/></Trigger>
    <Trigger Property="IsEnabled" Value="false">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
    <Setter Property="Background" Value="#FFF4F4F4"/></Trigger>
    <Trigger Property="IsGrouping" Value="true">
    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/></Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate></Setter.Value</Setter>
    <Style.Triggers>
    <Trigger Property="IsEditable" Value="true">
    <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/>
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <Setter Property="IsTabStop" Value="false"/>
    <Setter Property="Padding" Value="3"/>
    <Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}"/>
    </Trigger>
    </Style.Triggers>
    </Style>
    </Window.Resources>
    <StackPanel>
    <ComboBox Style="{DynamicResource ComboBoxStyle1}">
    <ComboBoxItem Content="abc"/>
    <ComboBoxItem Content="abc"/>
    <ComboBoxItem Content="abc"/>
    </ComboBox>
    </StackPanel>
    </Window>
于 2016-05-02T11:02:14.883 回答