按名称

时间:2016-03-18 15:59:07

标签: c# wpf user-controls

如果我的UserControl中包含以下两个Label的网格,请执行以下操作:

<Grid x:Name="mainGrid">
    <Label x:Name="labelTitle"/>
    <Label x:Name="labelValue"/>
</Grid>

我可以在ResourceDictionary之内单独设置样式:

<Style TargetType="{x:Type MyControl}">
    <Style.Resources>
        <Style TargetType="MyControl.mainGrid.labelTitle">
        </Style>
        <Style TargetType="MyControl.mainGrid.labelValue">
        </Style>
    </Style.Resources>
</Style>

如果可能,我想在ResourceDictionary中完成所有这些操作,而不必触及UserControl

1 个答案:

答案 0 :(得分:1)

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

的App.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>

MyControl.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>

MainWindow.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>

截图:

enter image description here