C# 如何在xaml中重写内联样式
我有一个绑定到ViewModel的TabControl。我想设置自动生成的TabPanel的边距,但我无法这样做,因为我认为样式是通过TabControl的实现内联设置的 这是我的风格C# 如何在xaml中重写内联样式,c#,.net,wpf,xaml,visual-studio-2015,C#,.net,Wpf,Xaml,Visual Studio 2015,我有一个绑定到ViewModel的TabControl。我想设置自动生成的TabPanel的边距,但我无法这样做,因为我认为样式是通过TabControl的实现内联设置的 这是我的风格 <TabControl.Resources> <Style TargetType="{x:Type TabPanel}"> <Setter Property="Margin" Value="14,0,0,0" /> </Style>
<TabControl.Resources>
<Style TargetType="{x:Type TabPanel}">
<Setter Property="Margin" Value="14,0,0,0" />
</Style>
</TabControl.Resources>
您需要覆盖TabControl模板以自定义外观。在这种情况下,您将负责设计模板,但也将能够以您喜欢的方式对其进行自定义 示例模板取自
您需要覆盖TabControl模板以自定义外观。在这种情况下,您将负责设计模板,但也将能够以您喜欢的方式对其进行自定义 示例模板取自
感谢您的回复。它通过一些小的调整解决了我的问题,但是有没有一个更短的解决方案呢?就像
!CSS中的重要信息
。我想应该有一个。@naveedbuttafaik,WPF样式与CSS样式不同。它们不是真正的级联,因此一次只对元素应用一种样式,并且应该没有冲突的属性!css中的要点与级联结合使用,以在发生冲突时提供最大权重。不过,您可能会遇到问题,就像您现在遇到的一样,wpf只是遵循链接中描述的规则。顺便说一句,这是一个类似的问题,但我不认为tecnique更好谢谢你的回答。它通过一些小的调整解决了我的问题,但是有没有一个更短的解决方案呢?就像!CSS中的重要信息
。我想应该有一个。@naveedbuttafaik,WPF样式与CSS样式不同。它们不是真正的级联,因此一次只对元素应用一种样式,并且应该没有冲突的属性!css中的要点与级联结合使用,以在发生冲突时提供最大权重。不过,您可能会遇到问题,就像您现在遇到的一样,wpf只是遵循链接中描述的规则。顺便说一句,这是一个类似的问题,但我不认为tecnique更好
<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>
<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>