Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to programmatically set `ContentControl.Content` as the dependency property for a data binding between a Button and a string property

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

It should be ContentProperty:

btnAddInt.SetBinding(ContentControl.ContentProperty, binding);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading