天天看点

UserControl给属性赋值失败的问题

UserControl给属性赋值失败的问题

自建了UserControl,自写了其属性。如下所示

public string Text
{
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }//先清空再赋值,这样不会出现赋值失败的问题
}

public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",
typeof(string), typeof(InforLabel), new PropertyMetadata("Text",
(sender, args) => { ((InforLabel)sender).txtBoxInforLabel.Text = args.NewValue.ToString(); }
));      
public string Text
{
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, ""); SetValue(TextProperty, value); }//先清空再赋值,这样不会出现赋值失败的问题
}