2016-04-30 123 views
1

我想在WPF中显示基于图像的组合框。它适用于一些模板,但我不知道如何摆脱blueish mousehover矩形。 见光的蓝色框:WPF如何在MouseOver上隐藏ComboBoxItem BorderBrush? (蓝色选择矩形)

enter image description here

OK我找到了解决办法,现在的问题是解决了,请参阅解决方案到这里:

解决方案:

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

您需要更改默认模板 –

+1

的问题我已经是物业我应该改变,并在什么模板? 我将添加剩下的代码,向您展示我已经尝试过的更多细节。 –

+0

好吧,发现如何做到这一点..我可以添加一个模板的comboboxItem无边框或不可见边框 –

回答

1

好发现如何做到这一点..我可以添加一个模板的comboboxItem没有边界或不可见边框。 我也更新了解决方案的问题。

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

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