2016-08-02 119 views
1

我有一個綁定到ViewModel的TabControl。我想設置產生的TabPanel汽車的利潤率,但我不能這樣做,因爲我覺得風格是通過的TabControl的實現在線設置......如何覆蓋xaml中的內聯樣式

這是我的風格。

<TabControl.Resources> 
    <Style TargetType="{x:Type TabPanel}"> 
     <Setter Property="Margin" Value="14,0,0,0" /> 
    </Style> 
</TabControl.Resources> 

和使用Visual Studio的Live物業Explorer是這樣產生的風格。

enter image description here

更新1:

這是XAML的ItemTemplate裏產生TabPanel本身:

<TabControl.ItemTemplate> 
    <DataTemplate> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition /> 
       <RowDefinition /> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition MaxWidth="150" /> 
      </Grid.ColumnDefinitions> 
      <Image Grid.Row="0" Grid.Column="0" Source="{Binding ImageUri}" 
        Height="25" Width="35" Margin="0,0,0,10" /> 
      <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding TestName}" TextAlignment="Center" 
         TextWrapping="Wrap" FontFamily="Open Sans" FontWeight="Regular" FontSize="14" /> 
     </Grid> 
    </DataTemplate> 
</TabControl.ItemTemplate> 

回答

0

你需要重寫TabControl的模板自定義外觀。在這種情況下,您將負責設計模板,但也能夠以您喜歡的方式進行自定義。

示例模板是從MSDN

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="" Width="500" Height="500"> 
<Window.Resources> 

    <Style TargetType="{x:Type TabControl}"> 
     <Setter Property="OverridesDefaultStyle" 
       Value="True" /> 
     <Setter Property="SnapsToDevicePixels" 
       Value="True" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TabControl}"> 
        <Grid KeyboardNavigation.TabNavigation="Local"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition Height="*" /> 
         </Grid.RowDefinitions> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" 
                     Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"> 
              <EasingColorKeyFrame KeyTime="0" Value="#FFAAAAAA" /> 
             </ColorAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <TabPanel x:Name="HeaderPanel" 
            Grid.Row="0" 
            Panel.ZIndex="1" 
            Margin="14,0,4,0" 
            IsItemsHost="True" 
            KeyboardNavigation.TabIndex="1" 
            Background="Transparent" /> 
         <Border x:Name="Border" 
           Grid.Row="1" 
           BorderThickness="1" 
           CornerRadius="2" 
           KeyboardNavigation.TabNavigation="Local" 
           KeyboardNavigation.DirectionalNavigation="Contained" 
           KeyboardNavigation.TabIndex="2"> 
          <Border.Background> 
           <LinearGradientBrush EndPoint="0.5,1" 
                StartPoint="0.5,0"> 
            <GradientStop Color="{DynamicResource ContentAreaColorLight}" Offset="0" /> 
            <GradientStop Color="{DynamicResource ContentAreaColorDark}" Offset="1" /> 
           </LinearGradientBrush> 
          </Border.Background> 
          <Border.BorderBrush> 
           <SolidColorBrush Color="{DynamicResource BorderMediumColor}" /> 
          </Border.BorderBrush> 
          <ContentPresenter x:Name="PART_SelectedContentHost" Margin="4" ContentSource="SelectedContent" /> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

</Window.Resources> 
<TabControl ItemsSource="{Binding Path=Items}"> 
    <TabControl.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition /> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition MaxWidth="150" /> 
       </Grid.ColumnDefinitions> 

       <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding .}" TextAlignment="Center" 
          TextWrapping="Wrap" FontFamily="Open Sans" FontWeight="Regular" FontSize="14" /> 
      </Grid> 
     </DataTemplate> 
    </TabControl.ItemTemplate> 
</TabControl> 

enter image description here

+0

採取感謝您的答覆。它通過很小的調整解決了我的問題,但是對於這個問題沒有更簡單的解決方案嗎?像CSS中的'!important'一樣。我想應該有一個。 –

+0

@NaveedButt AFAIK,WPF樣式與CSS樣式不同。它們並不是真正級聯的,所以只有**一個**樣式一次被應用到元素,並且它不應該有衝突的屬性。 !在CSS中重要的是在級聯時使用,以在發生衝突時提供最大的權重。儘管如此,您可能會擁有一個** [優先](https://msdn.microsoft.com/en-us/library/ms743230.aspx)**問題,就像您現在所做的那樣,並且wpf只需遵循鏈接。順便說一句,** [這裏](http://stackoverflow.com/q/16008484/5246145)**是一個類似的問題,但我不認爲tecnique更好 – 3615