Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在XAML中按名称设置usercontrol子项的样式?_C#_Wpf_Xaml_User Controls - Fatal编程技术网

C# 在XAML中按名称设置usercontrol子项的样式?

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

我在stackoverflow中看到了以下代码:

   <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"/> 

编辑:


我又读了一遍你的问题,似乎你想从
风格
中引用
目标
,而不是从
目标
中引用
风格
。是这样吗?这听起来很不自然,就像想从基类中知道派生类实例的名称一样。

接受您的答案:)