WPF Databinding Magic

I have an ObservableCollection <Foo> which is associated with an ItemsControl (basically displays a list).

Foo basically looks like this (there are other members, but it does not implement any interfaces or events):

class Foo
{
  public string Name { get; set; }
  //...
}

When the user clicks on an item, I open a dialog box in which the user can edit the Foo properties (tied to a small view model using the Foo property for the selected item), Xaml looks like this:

<TextBox Text="{Binding Foo.Name,Mode=TwoWay}"
    Grid.Column="1" Grid.Row="0" Margin="2" />

It's really strange when a user edits a name, the value in the list changes! (not during input, but after the focus leaves the field)

How it's done? I did not implement the INotifyPropertyChanged interface on a Foo object!

, - . , , , .


: casperOne ! , 404:

[..] WPF, WPF PropertyDescriptor, , CLR INotifyPropertyChanged . PropertyDescriptor.AddValueChanged(). , , , PropertyDescriptor.SetValue() ValueChanged ( , TextBlocks ListBox.

INotifyPropertyChanged, , . , .

+3
3

, , , , WPF , , , , .

, Name , WPF , , , , , .

+4

, . INotifyPropertyChanged , TextBox Name Foo.

0

Because they are attached to one object. If you change the binding to

{Binding Foo.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}

then they will be synchronized when the user enters text in the text box.

0
source

Source: https://habr.com/ru/post/1705198/


All Articles