2010-12-06 27 views
1

挨拶 WPF - Comboboxitems - 一般的な背景イメージ

私はWPFのコンボボックスについての質問があり、私は現在の状況をスケッチしましょう、私が持っている:要するに

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

いくつかのイメージを持つコンボボックスそれをComboboxItemとしてどのように私はそれを行うことができます私は1つの大きな背景画像(コンボボックスからの拡張ボックスのサイズ)を1つの背景画像のComboboxItemではなく、これらのすべての画像のために持っています。 ComboboxItem内の各画像は透明な背景を持つので、背景画像が表示されるはずです。

お読みいただきありがとうございます。

+0

矩形の代わりに親の罫線を使用します。私の答えを見る –

答えて

1

ComboBoxポップアップの背景を変更することは、Korin - Marek K.のように(わかっている限り)簡単ではありません。 ComboBoxを再テンプレートし、ポップアップでボーダーの背景を設定する必要があります。幸いにも、これはBlendへのアクセスが難しくありません。これを行うには、PresentationFramework.Aeroへの参照を追加する必要があります。

<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

コンボボックス用の独自のスタイルを作成し、このスタイルでテンプレートセクションを変更する必要があります。
この記事では、http://msdn.microsoft.com/en-us/library/ms752094.aspxを使用して、コンボボックスの独自のスタイルを作成する方法を見つけることができます。
ポップアップのバックグラウンドを変更するには、このスタイルでPART_Popupを変更する必要があります。

関連する問題