繁体   English   中英

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

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

假设我有一个用户控件MyControl ,其中包含一个TextBlock和一个Button 如果我想分别控制按钮和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();
        }
    }
}

然而,这很快就会变得非常乏味,而且我怀疑它的性能是否最佳。 相反,我想直接引用我的用户控件的子控件并直接设置它们的属性,如下所示:

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

我已经阅读了一些有关附加属性的信息,但并不真正了解如何:

  1. 我可以将这些附加到已经存在的控件,例如TextBlockButton ,我需要扩展它们吗?
  2. 我将如何 go 绑定这些并在MyControl中设置它们的默认值。

如果有人能给我一个例子,或者朝着正确的方向轻推,我将非常感激。

您可以在 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>

您甚至可以在控件的另一个 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>

请注意,您不应将 UserControl 的 DataContext 设置为自身。 控件属性的任何基于 DataContext 的绑定,例如

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

行不通。

暂无
暂无

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

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