2015-11-08 74 views
0

編輯:顏色覆蓋作品在我有一些按鈕,其工作方式相同,除了我沒有定義任何顏色的主要風格資源,我只是設置顏色標籤但沒有顏色的屬性BasedOn風格沒有壓倒一切的風格資源


以下樣式得到了3種顏色設置爲整個風格使用默認的顏色,但是我想在坐這3種顏色,如果我想要一個不同顏色的按鈕設置。

這裏是ComboBox的,第一個應該是默認顏色,第二個應該是綠色覆蓋。

<ComboBox HorizontalAlignment="Left" SelectedIndex="0" Text="Select USB Device"> 
      <ComboBoxItem Content="Test 1" /> 
      <ComboBoxItem Content="Test 2" /> 
      <ComboBoxItem Content="Test 3" /> 
     </ComboBox> 
     <ComboBox HorizontalAlignment="Left" SelectedIndex="0" Text="Select USB Device" Style="{StaticResource ComboBlue}"> 
      <ComboBoxItem Content="Test 1" /> 
      <ComboBoxItem Content="Test 2" /> 
      <ComboBoxItem Content="Test 3" /> 
     </ComboBox> 

XAML,主要風格

<!-- Combo Box Style --> 
    <Style TargetType="ComboBox"> 
     <Style.Resources> 
      <SolidColorBrush x:Key="colour1" Color="{Binding Source={StaticResource NorGrey}, Path=Color}" /> 
      <SolidColorBrush x:Key="colour2" Color="{Binding Source={StaticResource DarGrey}, Path=Color}" /> 
      <SolidColorBrush x:Key="colour3" Color="{Binding Source={StaticResource LigGrey}, Path=Color}" /> 

      <Style TargetType="ComboBoxItem"> 
       <Setter Property="SnapsToDevicePixels" Value="True" /> 
       <Setter Property="OverridesDefaultStyle" Value="True" /> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="ComboBoxItem"> 
          <Border x:Name="Border" 
            Padding="4" 
            SnapsToDevicePixels="True" 
            Background="Transparent"> 
           <ContentPresenter /> 
          </Border> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsMouseOver" Value="True"> 
            <Setter TargetName="Border" Property="Background" Value="{StaticResource colour3}" /> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 

      <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton"> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition /> 
         <ColumnDefinition Width="20" /> 
        </Grid.ColumnDefinitions> 
        <Border x:Name="Border" 
          Grid.ColumnSpan="2" 
          CornerRadius="5" 
          Background="{StaticResource colour1}"/> 
        <Border Grid.Column="0" 
          CornerRadius="5,0,0,5" 
          Margin="1" 
          Background="{StaticResource colour2}" /> 
        <Path x:Name="Arrow" 
          Grid.Column="1" 
          HorizontalAlignment="Center" 
          VerticalAlignment="Center" 
          Data="M 0 0 L 4 4 L 8 0 Z" 
          Fill="White" /> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter TargetName="Border" Property="Background" Value="{StaticResource colour3}" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 

      <ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox"> 
       <Border x:Name="PART_ContentHost" 
         Focusable="False" 
         Background="{StaticResource colour1}" /> 
      </ControlTemplate> 
     </Style.Resources> 
     <Setter Property="Margin" Value="5" /> 
     <Setter Property="Foreground" Value="White" /> 
     <Setter Property="SnapsToDevicePixels" Value="True" /> 
     <Setter Property="OverridesDefaultStyle" Value="True" /> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" /> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> 
     <Setter Property="ScrollViewer.CanContentScroll" Value="True" /> 
     <Setter Property="MinHeight" Value="20" /> 
     <Setter Property="MinWidth" Value="80" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ComboBox"> 
        <Grid> 
         <ToggleButton x:Name="ToggleButton" 
             Template="{StaticResource ComboBoxToggleButton}" 
             Grid.Column="2" 
             Focusable="False" 
             ClickMode="Press" 
             IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" /> 
         <ContentPresenter x:Name="ContentSite" 
              IsHitTestVisible="False" 
              Content="{TemplateBinding SelectionBoxItem}" 
              ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
              ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
              Margin="6,6,10,6" 
              VerticalAlignment="Stretch" 
              HorizontalAlignment="Left" /> 
         <TextBox x:Name="PART_EditableTextBox" 
           Style="{x:Null}" 
           Template="{StaticResource ComboBoxTextBox}" 
           HorizontalAlignment="Center" 
           VerticalAlignment="Bottom" 
           Margin="3,3,23,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" 
            SnapsToDevicePixels="True" 
            MinWidth="{TemplateBinding ActualWidth}" 
            MaxHeight="{TemplateBinding MaxDropDownHeight}"> 
           <Border x:Name="DropDownBorder" 
             Background="{StaticResource colour1}" /> 
           <ScrollViewer Margin="4,6,4,6" 
               SnapsToDevicePixels="True"> 
            <StackPanel IsItemsHost="True" 
               KeyboardNavigation.DirectionalNavigation="Contained" /> 
           </ScrollViewer> 
          </Grid> 
         </Popup> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="HasItems" Value="False"> 
          <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" /> 
         </Trigger> 
         <Trigger Property="IsGrouping" Value="True"> 
          <Setter Property="ScrollViewer.CanContentScroll" Value="True" /> 
         </Trigger> 
         <Trigger SourceName="Popup" Property="AllowsTransparency" Value="True"> 
          <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4" /> 
          <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

這裏是位於下方的主風格,用於覆蓋的顏色樣式。但由於某些原因,顏色不會改變。

<!-- Combo Box Colors --> 
    <Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="ComboBox" x:Key="ComboGreen"> 
     <Style.Resources> 
      <SolidColorBrush Color="{Binding Source={StaticResource NorGreen}, Path=Color}" x:Key="colour1" /> 
      <SolidColorBrush Color="{Binding Source={StaticResource DarGreen}, Path=Color}" x:Key="colour2" /> 
      <SolidColorBrush Color="{Binding Source={StaticResource LigGreen}, Path=Color}" x:Key="colour3" /> 
     </Style.Resources> 
    </Style> 
    <Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="ComboBox" x:Key="ComboGrey"> 
     <Style.Resources> 
      <SolidColorBrush Color="{Binding Source={StaticResource NorGrey}, Path=Color}" x:Key="colour1" /> 
      <SolidColorBrush Color="{Binding Source={StaticResource DarGrey}, Path=Color}" x:Key="colour2" /> 
      <SolidColorBrush Color="{Binding Source={StaticResource LigGrey}, Path=Color}" x:Key="colour3" /> 
     </Style.Resources> 
    </Style> 
    <Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="ComboBox" x:Key="ComboBlue"> 
     <Style.Resources> 
      <SolidColorBrush Color="{Binding Source={StaticResource NorBlue}, Path=Color}" x:Key="colour1" /> 
      <SolidColorBrush Color="{Binding Source={StaticResource DarBlue}, Path=Color}" x:Key="colour2" /> 
      <SolidColorBrush Color="{Binding Source={StaticResource LigBlue}, Path=Color}" x:Key="colour3" /> 
     </Style.Resources> 
    </Style> 
    <Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="ComboBox" x:Key="ComboRed"> 
     <Style.Resources> 
      <SolidColorBrush Color="{Binding Source={StaticResource NorRed}, Path=Color}" x:Key="colour1" /> 
      <SolidColorBrush Color="{Binding Source={StaticResource DarRed}, Path=Color}" x:Key="colour2" /> 
      <SolidColorBrush Color="{Binding Source={StaticResource LigRed}, Path=Color}" x:Key="colour3" /> 
     </Style.Resources> 
    </Style> 

回答

0

我已經設法找到解決方案,我使用了錯誤的資源類型。當參考我應該使用的顏色DynamicResource而不是StaticResource