2014-09-17 4 views
4

Я хотел бы изменить стиль по умолчанию для comboBox. Итак, я пишу этот код:Как установить Стиль WPF ComboBox

 <!-- ComboBox style --> 
     <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox"> 
      <Setter Property="SnapsToDevicePixels" Value="true"/> 
      <Setter Property="OverridesDefaultStyle" Value="true"/> 
      <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ComboBox"> 
         <Grid> 
          <ToggleButton Name="ToggleButton" 
        Template="{StaticResource ComboBoxToggleButton}" 
        IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" 
        ClickMode="Press"> 
          </ToggleButton> 
          <ContentPresenter Name="ContentSite" IsHitTestVisible="False" 
         Content="{TemplateBinding SelectionBoxItem}" 
         ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
         ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
         Margin="3,3,23,3" 
          <TextBox x:Name="PART_EditableTextBox" 
         Style="{x:Null}" Template="{StaticResource ComboBoxTextBox}" 
         Text="{TemplateBinding Text}" 
         Foreground="DarkBlue" 
         IsReadOnly="{TemplateBinding IsReadOnly}"/> 
          <Popup Name="Popup" Placement="Bottom" 
         IsOpen="{TemplateBinding IsDropDownOpen}" 
         AllowsTransparency="True" Focusable="False" 
         PopupAnimation="Slide"> 
           <Grid Name="DropDown" 
          SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" 
          MaxHeight="{TemplateBinding MaxDropDownHeight}"> 
            <Border x:Name="DropDownBorder"/> 
            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> 
             <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /> 
            </ScrollViewer> 
           </Grid> 
          </Popup> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
      </Style.Triggers> 
     </Style> 

Это мой COMBOBOX в XAML файле, но я не знаю, чтобы установить свой стиль:

<ComboBox x:Name="nomiGiocatori" /> 

Как применить новый тампон этого ComboBox?

Reguards

ответ

4

Ваш стиль немного неправильно, он должен быть

<Style x:Key="YourButtonStyle" TargetType="{x:Type Button}"> 

А в коде XAML

<ComboBox x:Name="nomiGiocatori" Style="{StaticResource YourButtonStyle}"/> 

Вы можете применить его ко всем Наример, если вы определили свой стиль как

<Style TargetType="{x:Type Button}" BasedOn="Button"/> 

Тогда вам не нужно ничего делать с вашим xaml.

This answer describes a similar issue.

Надеется, что это помогает,

Stian

Смежные вопросы