WPF自定义usercontrol属性绑定始终是默认值

时间:2019-05-28 16:44:22

标签: wpf wpf-controls

因此,我找到了几个答案,但是没有发现我的具体问题。在代码中看起来像这样:

UserControl

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly DependencyProperty TextProperty =
           DependencyProperty.Register("Text", typeof(string), typeof(NiceTextBox), new PropertyMetadata("default"));

public MyUserControl() {
    InitializeComponent();
    var whatIsMyText = Text;
}

XAML

<customControl:MyUserControl ... Text="sometext" />

whatIsMyText的内部没有"sometext",而是"default"。我确信这是由于初始化和实际设置写入xaml内的属性数据的时间所致。但是我该怎么做呢?

0 个答案:

没有答案