简体   繁体   English

如何绑定用户控件子项的属性

[英]How to bind properties of a usercontrol's children

Let's say I have a usercontrol MyControl , which contains a TextBlock and a Button .假设我有一个用户控件MyControl ,其中包含一个TextBlock和一个Button If I want to separately control the font size & weight, text, height, etc. of both the button and the TextBlock and the Button , I could create DependencyProperties for all this and bind it:如果我想分别控制按钮和TextBlockButton的字体大小和粗细、文本、高度等,我可以为所有这些创建 DependencyProperties 并绑定它:

<UserControl xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="ns.MyControl"
             DataContext={Binding RelativeSource={RelativeSource self}}>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text={Binding BlockText} Height={Binding BlockHeight}
                   FontWeight={Binding BlockFontWeight} FontSize={Binding BlockFontSize} />
        <Button Grid.Column="1" Text={Binding ButtonText} Height={Binding ButtonHeight}
                FontWeight={Binding ButtonFontWeight} FontSize={Binding ButtonFontSize} />
    </Grid>
</UserControl>
namespace ns {
    class MyControl : UserControl {
        private static readonly DependencyProperty BlockTextProperty = DependencyProperty.Register(
            "BlockText", typeof(string), typeof(MyControl));
        internal string BlockText {
            get { return (string)GetValue(BlockTextProperty); }
            set { SetValue(BlockTextProperty, value); }
        }
        ...
        private static readonly DependencyProperty ButtonFontSizeProperty= DependencyProperty.Register(
            "ButtonFontSizeProperty", typeof(string), typeof(MyControl));
        internal string ButtonFontSizeProperty{
            get { return (string)GetValue(ButtonFontSizeProperty); }
            set { SetValue(ButtonFontSizeProperty, value); }
        }

        public MyControl() {
            InitializeComponent();
        }
    }
}

However, this will quickly become very tedious, and I doubt it'd perform optimally.然而,这很快就会变得非常乏味,而且我怀疑它的性能是否最佳。 Rather, I'd like to directly refer to the children of my usercontrol and set their properties directly, something like this:相反,我想直接引用我的用户控件的子控件并直接设置它们的属性,如下所示:

<MyControl Block.Text="Foo" Block.FontWeight="Bold" Block.FontSize="16" Button.Height="40" />

I have read a bit about attached properties, but don't really understand how:我已经阅读了一些有关附加属性的信息,但并不真正了解如何:

  1. I could attach these to already existing controls such as TextBlock and Button , do I need to extend them?我可以将这些附加到已经存在的控件,例如TextBlockButton ,我需要扩展它们吗?
  2. How I would go about binding these and setting their default values inside MyControl .我将如何 go 绑定这些并在MyControl中设置它们的默认值。

I'd very much appreciate if someone can give me an example, or a nudge in the right direction.如果有人能给我一个例子,或者朝着正确的方向轻推,我将非常感激。

You may declare default Styles in the Resources of the UserControl:您可以在 UserControl 的资源中声明默认 Styles:

<local:MyControl>
    <local:MyControl.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Text" Value="Foo"/>
        </Style>
        <Style TargetType="Button">
            <Setter Property="Height" Value="40"/>
        </Style>
    </local:MyControl.Resources>
</local:MyControl>

You may even declare those child element Style resources in another Style for the control:您甚至可以在控件的另一个 Style 中声明这些子元素 Style 资源:

<Window.Resources>
    <Style TargetType="local:MyControl">
        <Style.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="FontSize" Value="16"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Text" Value="Foo"/>
            </Style>
            <Style TargetType="Button">
                <Setter Property="Height" Value="40"/>
            </Style>
        </Style.Resources>
    </Style>
</Window.Resources>
<Grid>
    <local:MyControl/>
</Grid>

As a note, you should not set the UserControl's DataContext to itself.请注意,您不应将 UserControl 的 DataContext 设置为自身。 Any DataContext based Bindings of the control's properties like控件属性的任何基于 DataContext 的绑定,例如

<local:MyControl FontSize="{Binding Something}">

would not work.行不通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM