2010-12-06 8 views
1

GrüßeWPF - Comboboxitems - Allgemeine Hintergrundbild

Ich habe eine Frage bezüglich der Combobox in WPF, lassen Sie mich die aktuelle Situation skizzieren, die ich habe:

<ComboBox IsEditable="False" Height="23" HorizontalAlignment="Left" Margin="8,112,0,0" Name="comboBox1" VerticalAlignment="Top" Width="140" Background="Transparent"> 
    <Image Source="pack://application:,,,/Images/Style/Player/Static/Red.png" Width="122" /> 
    <Image Source="pack://application:,,,/Images/Style/Player/Static/Blue.png" Width="122" /> 
    <Image Source="pack://application:,,,/Images/Style/Player/Static/Teal.png" Width="122" /> 
    <Image Source="pack://application:,,,/Images/Style/Player/Static/Purple.png" Width="122" /> 
    <Image Source="pack://application:,,,/Images/Style/Player/Static/Yellow.png" Width="122" /> 
    <Image Source="pack://application:,,,/Images/Style/Player/Static/Orange.png" Width="122" /> 
    <Image Source="pack://application:,,,/Images/Style/Player/Static/Green.png" Width="122" /> 
    <Image Source="pack://application:,,,/Images/Style/Player/Static/Pink.png" Width="122" /> 
</ComboBox> 

Kurz gesagt ein ComboBox mit ein paar Bilder in ihm als ComboboxItem. Wie kann ich es tun, so dass ich 1 großes Hintergrundbild (die Größe der erweiterten Box aus der Combobox) für alle diese Bilder anstelle von 1 Hintergrundbild PER ComboboxItem habe. Jedes Bild in einem ComboboxItem hat einen transparenten Hintergrund, sodass das Hintergrundbild sichtbar sein sollte.

Vielen Dank für das Lesen und Ihren möglichen Rat.

+0

Verwenden Sie den übergeordneten Rahmen anstelle des Rechtecks. Siehe meine Antwort –

Antwort

1

Ändern des Hintergrunds des ComboBox Popup ist nicht so einfach wie es klingt (so weit ich weiß) wie Korin - Marek K. sagte. Sie müssen die ComboBox neu erstellen und den Hintergrund für den Rand im Popup festlegen. Glücklicherweise ist dies mit Zugriff auf Blend nicht so schwierig. Sie müssen einen Verweis auf PresentationFramework.Aero hinzufügen, damit das funktioniert.

<Window ... 
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"> 
<Window.Resources> 
    <Geometry x:Key="DownArrowGeometry">M 0 0 L 3.5 4 L 7 0 Z</Geometry> 
    <Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}"> 
     <Setter Property="OverridesDefaultStyle" Value="true"/> 
     <Setter Property="IsTabStop" Value="false"/> 
     <Setter Property="Focusable" Value="false"/> 
     <Setter Property="ClickMode" Value="Press"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToggleButton}"> 
        <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="true"> 
         <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"> 
          <Path x:Name="Arrow" Data="{StaticResource DownArrowGeometry}" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center"/> 
         </Grid> 
        </Microsoft_Windows_Themes:ButtonChrome> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsChecked" Value="true"> 
          <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <ControlTemplate x:Key="comboBoxTemplate" 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" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"> 
       <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}"> 
        <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1"> 

         <!-- ONLY CHANGE MADE FROM ORIGINAL TEMPLATE --> 
         <Border.Background> 
          <ImageBrush ImageSource="pack://application:,,,/Images/Style/Player/Static/Red.png"/> 
         </Border.Background> 
         <!-- END OF CHANGE FROM ORIGINAL TEMPLATE --> 

         <ScrollViewer x:Name="DropDownScrollViewer"> 
          <Grid RenderOptions.ClearTypeHint="Enabled"> 
           <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> 
            <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/> 
           </Canvas> 
           <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
          </Grid> 
         </ScrollViewer> 
        </Border> 
       </Microsoft_Windows_Themes:SystemDropShadowChrome> 
      </Popup> 
      <ToggleButton BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxReadonlyToggleButton}"/> 
      <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
     </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> 
      <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false"> 
       <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/> 
       <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 
</Window.Resources> 

<ComboBox Template="{StaticResource comboBoxTemplate}" .../> 
1

Sie müssen Ihren eigenen Stil für Combobox schreiben und Template-Abschnitt in diesem Stil ändern.
In diesem Artikel http://msdn.microsoft.com/en-us/library/ms752094.aspx können Sie Ihren eigenen Stil für Combobox schreiben.
Um Hintergrund von Popup zu ändern, müssen Sie PART_Popup in diesem Stil ändern.