0

i'm beginner to C# and WPF and I had a question while studying. Data Binding with get, set is work well like below.

 public partial class Some: Window
    {
        public string Name { get; set; } = "NAME3";

        public Some()
        {
            InitializeComponent();
            this.DataContext = this;
        }

But, Data Binding without get,set dosen't work at all.

 public partial class Some: Window
    {
        public string Name = "NAME3";

        public Some()
        {
            InitializeComponent();
            this.DataContext = this;
        }

What is difference between them? Thank you

Dae
  • 1
  • You are not only changing get-set behaviour, in the 2nd code snippet you have a field and not anymore a propery. – KargWare Feb 22 '23 at 06:34
  • @KargWare I understand! "you have a field and not anymore a propery. " It means Data Binding is possible with 'property' but not with 'field', right? – Dae Feb 22 '23 at 06:57
  • Data Binding in WPF works with public properties only. You may want to read [Data Binding Overview](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/?view=netdesktop-7.0). – Clemens Feb 22 '23 at 07:16

1 Answers1

-2

Check the answer on SO - What are the various WPF binding modes?.

There are the different Binding-Modes listed.

When you want to go without the set may one-way is the solution. So only binding from model to view will happen. But that should be only for constant or readonly elemenets on the view.

KargWare
  • 1,746
  • 3
  • 22
  • 35