0
votes

I'm kinda new to data binding. I managed to do the one way databind but I have some trouble doing the two way one.

I made 1)a usercontrol with some textblocks in it, 2)a class with some string properties, 3)an ObservableCollection that uses this class to generate the objects with those properties,4)a listview that generated by the ObservableCollection and that has the usercontrol as datatemplate for the items.

to bind a property of the class with a usercontrol's TextBlock I wrote the following code in XAML:

<TextBlock x:Name="MyTextBlock" Text="{Binding TextBlock_Property}" />

Where MyTextBlock is a textblock inside the usercontrol and TextBlock_Propertyis one of the properties from the class I created. I also tried Text="{Binding TextBlock_Property, Mode=TwoWay}" but I didn't see any difference.

Note: When I change the property of a created object, the textblock changes too, but when I change the Textblock content the property doesn't update.

Update:The class I made is

class MyClass
{
    public string Title { get; set; }
    public string TextBlock_Property { get; set; }

    public MyClass(string title, string textBlock_Property)
    {
        Title = title;
        TextBlock_Property = textBlock_Property;
    }
}
1
W8 I'm going to search this Dependency property because not sure what it is, haven't used anything like that.user2975038
I don't think this is a dependency property issue. It looks like he is just binding to a property. Can we see the VM or code behind that has your properties? And where you set your datacontext if you do?Matthew Frontino
@user2975038 what do you mean by change the Textblock content? TextBlock is read-only control and does not allow user inputdkozl
@user2975038 You should not be setting TextBlock.Text = "Something" when working with databound objects in WPF, you should be setting MyClass.TextBlock_Property = "Something" where MyClass is the datacontext for the TextBlock object and the source for the Text property bindingRachel

1 Answers

1
votes

The class MyClass has to implement INotifyPropertyChanged and the property TextBlock_Property has signal a OnPropertyChanged("TextBlock_Property") event for the binding to be updated.

    private string _TextBlock_Property;

    public string TextBlock_Property
    {
        get { return _TextBlock_Property; }
        set { _TextBlock_Property = value; OnPropertyChanged("TextBlock_Property"); }
    }

To notify the Xaml/bound controls that data has changed one must implement INotifyPropertyChange on the class which holds the properties, and its instance is in the DataContext. Below is code I use to implement the interface INotifyPropertyChanged.

public class MyClass : INotifyPropertyChanged
{
        /// <summary>
        /// Event raised when a property changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The name of the property that has changed.</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
}

To see this operation in its full, check out my blog article Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding.