Home > Net >  How to programmatically set `ContentControl.Content` as the dependency property for a data binding b
How to programmatically set `ContentControl.Content` as the dependency property for a data binding b

Time:10-29

I'm trying to create a simple data binding programmatically in a minimal WPF application. The source is the SourceText property in the SourceClass instance and the target is the Button control:

XAML File:

namespace notify_on_source_updated
{

    public class SourceClass
    {
        public string SourceText { get; set; } 
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();
            Binding binding = new Binding("SourceText");

            binding.Source = new SourceClass() { SourceText = "test" };
            binding.NotifyOnSourceUpdated=true;
            
            btnAddInt.SetBinding(ContentControl.Content, binding);
        }

    }
}

Unfortunately, when I run this I get an error:

An object reference is required for the non-static field, method, or property 'ContentControl.Content'

As far as I can tell the ContentControl.Content is the correct dependency property here. What am I doing wrong?

CodePudding user response:

It should be ContentProperty:

btnAddInt.SetBinding(ContentControl.ContentProperty, binding);
  • Related