簡體   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