简体   繁体   中英

Binding multiple TextBoxes to different properties of one Object, MVVM, C#

Here is my object in the Model:

public class Object
{
   public int Id { get; set; }
   public string Property1 { get; set; }
   public string Property2 { get; set; }
}

And here is my object in the ViewModel:

private Object_object;

public string Object
{
   get { return _object; }
   set 
   { 
       _object = value; 
       NotifyOfPropertyChange(() => Object);
   }
}

I am using Caliburn Micro as my MVVM framework.

Here is my XAML in the View:

<TextBox Text="{Binding Object.Property1, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"/>

I would like to bind the TextBox value to a property of an object. This way when I pass the object to another ViewModel, I am passing one Object, not numerous properties. Is this possible, or do I need to just create a new property as type string ?

First off, declaring your class "Object" is basically overwriting the DEFAULT base class of Object. Give it some more realistic name even if 'MyTestObject'

Define your custom class

public class MyTestObject
{
   public int Id { get; set; }
   public string Property1 { get; set; }
   public string Property2 { get; set; }
}

Implement in your view model

private MyTestObject _myBindToTestObject;

public MyTestObject MyBindToTestObject
{
   get { return _myBindToTestObject; }
   set 
   { 
       _myBindToTestObject = value; 
       NotifyOfPropertyChange(() => MyTestObject);
   }
}

Finally bind to the form

<TextBox Text="{Binding MyBindToTestObject.Property1, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"/>

Binding goes to the publicly exposed object name "MyBindToTestObject" and then the specific property on that object.

You need to invoke NotifyOfPropertyChanged each time the individual Property of your Custom Object Changes. For example,

public class Foo : PropertyChangedBase
{
        private string property1;
        private string property2;
        private int id;

        public int Id
        {
            get => id; 
            set
            {
                id = value;
                NotifyOfPropertyChange(nameof(Id));
            }
        }
        public string Property1
        {
            get => property1; 
            set
            {
                property1 = value;
                NotifyOfPropertyChange(nameof(Property1));
            }
        }
        public string Property2
        {
            get => property2; 
            set
            {
                property2 = value;
                NotifyOfPropertyChange(nameof(property2));
            }
        }
}

Now you could use the binding to the individual property of the Custom Object.

<TextBox Text="{Binding FooInstance.Property2}"></TextBox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM