C# 在XAML中按名称设置usercontrol子项的样式?
我在stackoverflow中看到了以下代码:C# 在XAML中按名称设置usercontrol子项的样式?,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我在stackoverflow中看到了以下代码: <UserControl x:Class="WpfApplication3.UserControl1" x:Name="Uc1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com
<UserControl x:Class="WpfApplication3.UserControl1"
x:Name="Uc1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<Style TargetType="Label">
<Setter Property="Foreground"
Value="{Binding Foreground, ElementName=Uc1, Mode=OneWay}"/>
</Style>
</UserControl.Resources>
<Grid>
<Label Content="Label 1"/>
<Label Content="Label 2"/>
</Grid>
</UserControl>
问题:我现在想知道是否可以在usercontrol.resources中针对特定标签进行样式设置。在我的用户控制范围内可能吗?如果是,那么如何将不带
键的样式应用于范围内目标类型的所有实例
为样式指定一个键
,如
<Style TargetType="Label" x:Key="MyLabel">
然后按如下方式使用该键
<Label Content="Label 1" Style="{StaticResource MyLabel}" />
<!--Will not apply the style to Label 2-->
<Label Content="Label 2"/>
编辑:
我又读了一遍你的问题,似乎你想从风格
中引用目标
,而不是从目标
中引用风格
。是这样吗?这听起来很不自然,就像想从基类中知道派生类实例的名称一样。接受您的答案:)