Accessing WPF UserControl child element property

前端 未结 1 968
醉酒成梦
醉酒成梦 2021-01-17 08:17

Let\'s say I have a UserControl with several child controls


        
相关标签:
1条回答
  • 2021-01-17 08:32

    Try like this (although it's not the best practice to style controls in their parent control):

    <local:AnyControl>
        <local:AnyControl.Resources>
            <Style TargetType="{x:Type Label}">
                <Setter Property="Background" Value="Red" />
            </Style>
        </local:AnyControl.Resources>
    </local:AnyControl>
    

    It sets the background property for all controls of a given type inside your UserControl. If you want to change it for a control selected by a name, you can do something like that (change Value="Test" to your control's name):

    <local:AnyControl>
        <local:AnyControl.Resources>
            <Style TargetType="{x:Type Label}">
                <Style.Triggers>
                    <Trigger Property="Name" Value="Test">
                        <Setter Property="Background" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </local:AnyControl.Resources>
    </local:AnyControl>
    
    0 讨论(0)
提交回复
热议问题