2013-03-18 25 views
2

我只需要在ComboBox周圍添加厚邊框。正如你可能已經知道的那樣,ComboBox的BorderThickness屬性沒有多大用處。所以我想用下面的樣式修改Template,但不能找出我需要的Border標籤來表示ComoboBox本身裏面寫:使用樣式略微修改控件模板

<Style TargetType="{x:Type ComboBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ComboBox"> 
       <Border BorderBrush="Black" BorderThickness="2"> 
        WHAT GOES HERE? 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

我試圖ContentPresenterContentControl但老實說唐在這個特定的場景中,他們不太瞭解他們的使用情況。

+1

相關問題:[WPF - styling comboboxes](http://stackoverflow.com/q/2461883/302677) – Rachel 2013-03-18 14:59:33

+0

@Rachel:正確。不知道我是如何錯過的。 – dotNET 2013-03-18 16:18:43

回答

0

您必須編輯完整的controlTemplate。這是一個可以隨意擴展的小例子。對於切換按鈕 首先一個的ControlTemplate打開的組合框:

<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}"> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition /> 
       <ColumnDefinition Width="15" /> 
      </Grid.ColumnDefinitions> 
      <Border x:Name="Border" Grid.ColumnSpan="2" BorderThickness="1" /> 
      <Path x:Name="Arrow" 
       Fill="Black" 
       SnapsToDevicePixels="True" 
       StrokeThickness="1" 
       VerticalAlignment="Center" 
       HorizontalAlignment="Center" 
       Grid.Column="1" 
       Data="M0,0 L0,3 L1,3 L1,4 L2,4 L2,5 L3,5 L3,6 L4,6 L4,5 L5,5 L5,4 L6,4 L6,3 L7,3 L7,0 L6,0 L6,1 L5,1 L5,2 L4,2 L4,3 L3,3 L3,2 L2,2 L2,1 L1,1 L1,0 L0,0 Z" /> 
     </Grid> 
     <ControlTemplate.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Background" Value="Gray" TargetName="Border"/> 
       <Setter Property="BorderBrush" Value="DarkGray" TargetName="Border"/> 
      </Trigger> 
      <Trigger Property="IsFocused" Value="true"> 
       <Setter Property="BorderBrush" Value="DarkGray" TargetName="Border"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 

這裏是爲使用該模板組合框的樣式。

<Style TargetType="{x:Type ComboBox}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ComboBox}"> 
        <Border CornerRadius="5" BorderThickness="2" BorderBrush="Red"> 
         <Grid> 
         <ToggleButton x:Name="ToggleButton" 
             Grid.Column="2" 
             Focusable="false" 
             ClickMode="Press" 
             IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
             Template="{StaticResource ComboBoxToggleButton}"/> 
         <ContentPresenter x:Name="ContentSite" 
             IsHitTestVisible="False" 
             Content="{TemplateBinding SelectionBoxItem}" 
             ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
             ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
             Margin="3,3,21,3" 
             VerticalAlignment="Stretch" 
             HorizontalAlignment="Left"> 
         </ContentPresenter> 
         <TextBox x:Name="PART_EditableTextBox" 
           Style="{x:Null}" 
           HorizontalAlignment="Left" 
           VerticalAlignment="Bottom" 
           Margin="3,3,21,3" 
           Focusable="True" 
           Background="Transparent" 
           Visibility="Hidden" 
           IsReadOnly="{TemplateBinding IsReadOnly}" /> 
         <Popup x:Name="Popup" 
          Placement="Bottom" 
          IsOpen="{TemplateBinding IsDropDownOpen}" 
          AllowsTransparency="True" 
          Focusable="False" 
          PopupAnimation="Slide"> 
          <Grid x:Name="DropDown" 
            Background="WhiteSmoke" 
            SnapsToDevicePixels="True" 
            MinWidth="{TemplateBinding ActualWidth}" 
            MaxHeight="{TemplateBinding MaxDropDownHeight}"> 
            <Border x:Name="DropDownBorder" BorderThickness="1" Margin="0 0 2 0" /> 
            <ScrollViewer SnapsToDevicePixels="True"> 
             <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /> 
            </ScrollViewer> 
          </Grid> 
         </Popup> 
         </Grid> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

希望它有幫助。

+0

好的,這就是它的樣子,如果OP遵循我的鏈接。 ; o) – DHN 2013-03-18 15:20:43

+0

@Olimpiu:非常感謝。正是我需要的。雖然它比我預期的要多一點工作。 – dotNET 2013-03-18 16:18:12

1

那麼如果我找到你的權利,你想把原來的ComboBox在那Border,對不對?你可以找到一個示例模板定製here
所以,如果你複製你的Border中的重要部分(Grid),它應該看起來像標準ComboBox與更厚Border。也許你將不得不做一些小的修改,以便它看起來很完美。順便說一句,MS混合將是一個很大的幫助。

+0

:非常感謝DHN。我想你和Olimpiu提出了同樣的事情,但他繼續提供實際的實施。無論如何,我想你的答案是第一個出現並值得讚賞的人。 – dotNET 2013-03-18 16:20:09

+0

嗯,我不喜歡發佈複製和粘貼解決方案。但無論如何,很高興我們可以幫助你。 ; o) – DHN 2013-03-18 17:16:33