2012-05-12 32 views
2

我想在我的應用程序中使用皇家主題的所有TextBlock控件都具有默認樣式。例如:在不影響組合框的情況下爲TextBlock應用默認樣式

<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}"> 
    <Setter Property="Margin" Value="4"/>   
</Style> 

這個偉大的工程,從皇家主題繼承但不幸的是還影響是內部像ComboBox其他控件的模板中使用TextBlock的風格,我不希望出現這種情況。

我已經試圖將我的風格降低一級(例如在Grid.Resources之內),但沒有運氣。

有沒有辦法做到這一點?當然,我不想指定我的風格(使用x:key),因爲我不想明確地向我的應用程序的每個TextBlock應用自定義樣式。沒有創建子類來覆蓋那裏的風格。

任何想法?先謝謝你!

P.S.我已經看過可能類似的問題,但沒有答案爲我工作。

編輯:

感謝所有的答案。我希望有一些我錯過的方式,可以區分在其他控件模板中使用的TextBlock

回答

1

通過這種排斥:

當然,我不希望我的名字風格(用x:鍵),因爲我不 希望應用到我的應用程序自定義的每一個的TextBlock 風格。並且在那裏不用創建子類來覆蓋樣式 。

你幾乎問不可能。您創建的樣式將應用於包含該樣式的字典下的可視樹中的所有TextBlocks。

如果你想做例外,你將不得不以某種方式指定它們。

顯然你的要求「對所有TextBlock控件都有一個默認樣式」是不正確的。你不希望所有的TextBlocks都有這種風格。

如果您想要從該級別應用的樣式,將樣式向下移動一個或多個級別可能是一種解決方案。

沒有辦法指定您想將樣式應用於特定的TextBlocks而不是其他人,除非您能夠以某種方式區分它們。

0

如果您熟悉HTML和CSS樣式。例如,在所有元素中都有特定的CSS風格是很常見的。 <div class="foo bar"> <input class="boo bar"特別是在專業/高級主題。

WPF和XAML實現相同的概念。不使用x:key的樣式無法滿足複雜應用UI的所有需求。

用於較少寫入爲combobox定義數據模板並應用特定的樣式。

<ComboBox ItemsSource="{Binding Items}"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <TextBox Style="{StaticResource myComboBoxStyle}" Text="{Binding Path}" /> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
2

(對不起我的英文不好)

一種方式是「力量」組合框和ComboBoxItem使用另一種風格的內部TextBlock的。這是不是很漂亮,但它的工作原理:)

只需添加到您的ResourceDictionary,現在你可以調整TextBlock的外觀的ComboBox裏面反正你想

<!-- Default for all TextBlock --> 
    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}"> 
    <Setter Property="Margin" Value="4"/> 
    <Setter Property="Foreground" Value="Green"/> 
</Style> 

<!-- Applied only to TextBlocks inside ComboBox --> 
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}" x:Key="ComboBoxTextBlockStyle"> 
    <Setter Property="Margin" Value="0"/> 
    <Setter Property="Foreground" Value="Blue"/> 
</Style> 


<Style TargetType="{x:Type ComboBox}"> 
    <Setter Property="FocusVisualStyle"> 
     <Setter.Value> 
      <Style> 
       <Setter Property="Control.Template"> 
        <Setter.Value> 
         <ControlTemplate> 
          <Rectangle Margin="4,4,21,4" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> 
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
    <Setter Property="BorderBrush" Value="#FFA7A6AA"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="Padding" Value="0,1"/> 
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="ScrollViewer.CanContentScroll" Value="True"/> 
    <Setter Property="ScrollViewer.PanningMode" Value="Both"/> 
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ComboBox}"> 

       <Grid SnapsToDevicePixels="True"> 

        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1"> 
         <Grid Grid.IsSharedSizeScope="True"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="1"/> 
           <ColumnDefinition Width="*"/> 
           <ColumnDefinition SharedSizeGroup="ComboBoxButton" Width="Auto"/> 
          </Grid.ColumnDefinitions> 
          <Border x:Name="SelectedItemBorder" Grid.ColumnSpan="2" Margin="{TemplateBinding Padding}"/> 
          <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" Grid.Column="1" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> 

           <!-- ADD THIS HERE (SELECTED ITEM)--> 
           <ContentPresenter.Resources> 
            <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource ComboBoxTextBlockStyle}"/> 
           </ContentPresenter.Resources> 

          </ContentPresenter> 
          <ToggleButton Grid.ColumnSpan="3" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> 
           <ToggleButton.Style> 
            <Style TargetType="{x:Type ToggleButton}"> 
             <Setter Property="MinWidth" Value="0"/> 
             <Setter Property="MinHeight" Value="0"/> 
             <Setter Property="Width" Value="Auto"/> 
             <Setter Property="Height" Value="Auto"/> 
             <Setter Property="Background" Value="Transparent"/> 
             <Setter Property="Focusable" Value="False"/> 
             <Setter Property="ClickMode" Value="Press"/> 
             <Setter Property="Template"> 
              <Setter.Value> 
               <ControlTemplate TargetType="{x:Type ToggleButton}"> 
                <Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
                 <Grid.ColumnDefinitions> 
                  <ColumnDefinition Width="*"/> 
                  <ColumnDefinition SharedSizeGroup="ComboBoxButton" Width="Auto"/> 
                 </Grid.ColumnDefinitions> 
                 <Microsoft_Windows_Themes:ScrollChrome x:Name="Chrome" Grid.Column="1" HasOuterBorder="False" Padding="1,0,0,0" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsChecked}" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="DownArrow" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/> 
                </Grid> 
               </ControlTemplate> 
              </Setter.Value> 
             </Setter> 
            </Style> 
           </ToggleButton.Style> 
          </ToggleButton> 
         </Grid> 
        </Border> 
        <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"> 
         <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}"> 
          <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"> 
           <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> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsSelectionBoxHighlighted" Value="True"/> 
          <Condition Property="IsDropDownOpen" Value="False"/> 
         </MultiTrigger.Conditions> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
        </MultiTrigger> 
        <Trigger Property="IsSelectionBoxHighlighted" Value="True"> 
         <Setter Property="Background" TargetName="SelectedItemBorder" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
        </Trigger> 
        <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="MinHeight" TargetName="DropDownBorder" Value="95"/> 
        </Trigger> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="BorderBrush" TargetName="Bd" Value="#FF335EA8"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
        </Trigger> 
        <Trigger Property="IsGrouping" Value="True"> 
         <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 
        </Trigger> 
        <Trigger Property="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> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsEditable" Value="True"> 
      <Setter Property="IsTabStop" Value="False"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ComboBox}"> 

         <Grid SnapsToDevicePixels="True"> 
          <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1"> 
           <Grid Grid.IsSharedSizeScope="True"> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition Width="1"/> 
             <ColumnDefinition Width="*"/> 
             <ColumnDefinition SharedSizeGroup="ComboBoxButton" Width="Auto"/> 
            </Grid.ColumnDefinitions> 
            <TextBox x:Name="PART_EditableTextBox" Grid.Column="1" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"> 
             <TextBox.Style> 
              <Style TargetType="{x:Type TextBox}"> 
               <Setter Property="OverridesDefaultStyle" Value="True"/> 
               <Setter Property="AllowDrop" Value="True"/> 
               <Setter Property="MinWidth" Value="0"/> 
               <Setter Property="MinHeight" Value="0"/> 
               <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
               <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> 
               <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
               <Setter Property="Template"> 
                <Setter.Value> 
                 <ControlTemplate TargetType="{x:Type TextBox}"> 
                  <ScrollViewer x:Name="PART_ContentHost" Background="Transparent" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" /> 
                 </ControlTemplate> 
                </Setter.Value> 
               </Setter> 
              </Style> 
             </TextBox.Style> 
            </TextBox> 
            <ToggleButton Background="{x:Null}" Grid.ColumnSpan="3" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> 
             <ToggleButton.Style> 
              <Style TargetType="{x:Type ToggleButton}"> 
               <Setter Property="MinWidth" Value="0"/> 
               <Setter Property="MinHeight" Value="0"/> 
               <Setter Property="Width" Value="Auto"/> 
               <Setter Property="Height" Value="Auto"/> 
               <Setter Property="Background" Value="Transparent"/> 
               <Setter Property="Focusable" Value="False"/> 
               <Setter Property="ClickMode" Value="Press"/> 
               <Setter Property="Template"> 
                <Setter.Value> 
                 <ControlTemplate TargetType="{x:Type ToggleButton}"> 
                  <Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
                   <Grid.ColumnDefinitions> 
                    <ColumnDefinition Width="*"/> 
                    <ColumnDefinition SharedSizeGroup="ComboBoxButton" Width="Auto"/> 
                   </Grid.ColumnDefinitions> 
                   <Microsoft_Windows_Themes:ScrollChrome x:Name="Chrome" Grid.Column="1" HasOuterBorder="False" Padding="1,0,0,0" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsChecked}" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="DownArrow" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/> 
                  </Grid> 
                 </ControlTemplate> 
                </Setter.Value> 
               </Setter> 
              </Style> 
             </ToggleButton.Style> 
            </ToggleButton> 
           </Grid> 
          </Border> 
          <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"> 
           <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}"> 
            <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"> 
             <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> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="HasItems" Value="False"> 
           <Setter Property="MinHeight" TargetName="DropDownBorder" Value="95"/> 
          </Trigger> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter Property="BorderBrush" TargetName="Bd" Value="#FF335EA8"/> 
          </Trigger> 
          <Trigger Property="IsEnabled" Value="False"> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
           <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
          </Trigger> 
          <Trigger Property="IsGrouping" Value="True"> 
           <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 
          </Trigger> 
          <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="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> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
    </Style.Triggers> 
</Style> 



<Style TargetType="{x:Type ComboBoxItem}"> 
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/> 
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/> 
    <Setter Property="Padding" Value="3,0"/> 
    <Setter Property="Background" Value="Transparent"/> 

    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
       <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True"> 
        <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> 

         <!-- ADD THIS HERE (ITEMS LIST DROPDOWN)--> 
         <ContentPresenter.Resources> 
          <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource ComboBoxTextBlockStyle}"/> 
         </ContentPresenter.Resources> 

        </ContentPresenter> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsHighlighted" Value="True"> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
相關問題