2016-08-02 37 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