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_Property
is 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;
}
}
TextBlock
is read-only control and does not allow user input – dkozlTextBlock.Text = "Something"
when working with databound objects in WPF, you should be settingMyClass.TextBlock_Property = "Something"
whereMyClass
is the datacontext for the TextBlock object and the source for the Text property binding – Rachel