天天看點

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); }//先清空再指派,這樣不會出現指派失敗的問題
}