按名称设置 UserControl 的特定元素的样式

The*_*der 1 c# wpf user-controls

如果我的网格中有UserControl以下两个,如下所示:Label

<Grid x:Name="mainGrid">
    <Label x:Name="labelTitle"/>
    <Label x:Name="labelValue"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

我可以在类似以下内容中单独设置它们的样式吗ResourceDictionary

<Style TargetType="{x:Type MyControl}">
    <Style.Resources>
        <Style TargetType="MyControl.mainGrid.labelTitle">
        </Style>
        <Style TargetType="MyControl.mainGrid.labelValue">
        </Style>
    </Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)

如果可能的话,我想在其中完成所有这些工作,而根本ResourceDictionary不需要接触。UserControl

J.H*_*.H. 5

尝试使用基于名称的样式的触发器。

应用程序.xaml

<Application x:Class="WpfApplication34.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication34"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type local:MyControl}">
            <Style.Resources>
                <Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
                    <Style.Triggers>
                        <Trigger Property="Name" Value="labelTitle">
                            <Setter Property="Content" Value="This is the Title" />
                            <Setter Property="HorizontalAlignment"  Value="Left" />
                        </Trigger>
                        <Trigger Property="Name" Value="labelValue">
                            <Setter Property="Content" Value="This is the Value" />
                            <Setter Property="HorizontalAlignment"  Value="Right" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Style.Resources>
        </Style>
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

我的控件.xaml

<UserControl x:Class="WpfApplication34.MyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication34"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid x:Name="mainGrid">
        <Label x:Name="labelTitle" />
        <Label x:Name="labelValue" />
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

主窗口.xaml

<Window x:Class="WpfApplication34.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication34"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:MyControl />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

截屏:

在此输入图像描述