我正在查看这个问题,并发现绑定Label.Content到非字符串值将应用隐式TextBlock样式,但是绑定到字符串则不会.
以下是重现问题的示例代码:
<Window.Resources>
<Style TargetType="Label">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Margin" Value="10"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Margin" Value="10"/>
</Style>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding SomeString}" Background="Red"/>
<Label Content="{Binding SomeDecimal}" Background="Green"/>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
绑定值的代码是
SomeDecimal = 50;
SomeString = SomeDecimal.ToString();
Run Code Online (Sandbox Code Playgroud)
最终结果如下所示,Margin隐式TextBlock样式的属性应用于仅绑定到非字符串的Label:

两个标签都呈现为
<Label>
<Border>
<ContentPresenter>
<TextBlock />
</ContentPresenter>
</Border>
</Label>
Run Code Online (Sandbox Code Playgroud)
当我使用Snoop检查VisualTree时,我可以看到它对于两个元素看起来完全相同,除了第二个TextBlock从隐式样式应用Margin,而第一个没有.

我已经使用Blend来提取默认标签模板的副本,但是没有看到任何奇怪的东西,当我将模板应用于我的两个标签时,同样的事情发生了.
<Label.Template>
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding …Run Code Online (Sandbox Code Playgroud) 在我的App.xaml中,我有一些隐式样式
<Style TargetType="{x:Type Button}">
...Blah...
</Style>
Run Code Online (Sandbox Code Playgroud)
只要它们不在我创建的自定义控件中,这些样式就可以用于控制.
public class NavigationControl : Control
{
public static readonly DependencyProperty ButtonStyleProperty =
DependencyProperty.Register("ButtonStyle", typeof(Style), typeof(NavigationControl));
public Style ButtonStyle
{
get { return (Style)GetValue(ButtonStyleProperty); }
set { SetValue(ButtonStyleProperty, value); }
}
}
static NavigationControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NavigationControl), new FrameworkPropertyMetadata(typeof(NavigationControl)));
}
public NavigationControl()
{
}
Run Code Online (Sandbox Code Playgroud)
<ControlTemplate x:Key="NavigationControlTemplate" TargetType="{x:Type controls:NavigationControl}">
<Button Style="{TemplateBinding ButtonStyle}"
</ControlTemplate>
<Style x:Key="DefaultButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="MinWidth" Value="75"/>
<Setter Property="Height" Value="50"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="-1"/>
</Style>
<Style …Run Code Online (Sandbox Code Playgroud) 我在努力实现一些应该是微不足道的事情上遇到了很大的困难.我正在使用全局XAML资源文件中定义的隐式按钮样式.我只想用ColorAnimation 将聚焦按钮的背景颜色更改为红色.我已经尝试了许多不同的组合Storyboard.TargetProperty,Storyboard.TargetName而且没有任何效果.我怎样才能做到这一点?
提前致谢.
<Style TargetType="Button" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused" >
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Button" From="Green" To="Red" Duration="00:00:01" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
...
Run Code Online (Sandbox Code Playgroud) 我想应用于左侧边栏(第一个)上的Padding="0"每个控件,以便它与每个控件(以及其他控件)左对齐。LabelStackPanelTextBox
如何定义<ApplicationResources>仅适用于特定容器内的元素的隐式样式?
备择方案:
x:Key="sidebarLabel"。然而,对于我的实际应用程序侧栏中的许多标签来说,这个选项似乎是多余的。Padding=0Label到侧边栏中的每个。这与之前的替代方案本质上相同。<StackPanel.Resources>. 但是,我希望将样式(在 中App.xaml)与 XAML(在 中MainWindow.xaml)分开。
<Application.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Padding" Value="0" />
</Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<GroupBox Header="New User">
<StackPanel>
<Label>First Name:</Label>
<TextBox/>
<Label>Last Name:</Label>
<TextBox/>
</StackPanel>
</GroupBox>
</StackPanel>
<GroupBox Grid.Column="1" Header="Main">
<StackPanel>
<Label>I want default padding here</Label>
</StackPanel>
</GroupBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)