WPF DependencyProperties

前端 未结 6 2024
孤城傲影
孤城傲影 2021-01-05 18:12

I have just realized I\'ve been coercing binding/dependency properties and not really fundamentally understanding the concept.

Heres the dependency property:

6条回答
  •  失恋的感觉
    2021-01-05 18:47

    Its a Window this is set in.

    public partial class Window1 : Window
    {
        public string Problem
        {
            get { return (string)GetValue(ProblemProperty); }
            set { SetValue(ProblemProperty, value); }
        }
    
        public static readonly DependencyProperty ProblemProperty =
                        DependencyProperty.Register(
                        "Problem",
                        typeof(string),
                        typeof(Window1));
    
    
        public Window1()
        {
            InitializeComponent();
    
            Problem = "ifowiof";
        }
    
        public void OnClick(object sender, EventArgs e)
        {
            Problem = "behl";
        }
    
        public void OnCancel(object sender, EventArgs e)
        {
           Problem = "eioeopje";
        }
    }
    

    XAML:

    
            
                

    It works if I set the RelativeSource like you said when it loads, but if i change the Problem property in code manually (ie. via a button click) it never updates the TextBlock with the new value.

提交回复
热议问题