S
S
Sergey2022-02-03 23:06:40
WPF
Sergey, 2022-02-03 23:06:40

Why doesn't the source update the value of the destination in WPF?

I'm learning WPF, and specifically the topic of "bindings", and decided to put them into practice. I tried to implement the following example:

Let there be a Phone class (as you can see, dependency properties are not used)

public class Phone
    {
        public string Title { get; set; }
        public int Price { get; set; }
    }


In the window resources, I add this object and set its property values:
<Window.Resources>
        <local:Phone Title="Xiaomi" Price="10000" x:Key="myPhone"/>
    </Window.Resources>


And this is how the data is output:
<TextBlock Grid.Column="0" Grid.Row="0" Text="Модель:"/>
<TextBlock Grid.Column="1" Grid.Row="0" Text="Цена:"/>
<TextBox Grid.Column="0" Grid.Row="1" Text="{Binding Source={StaticResource myPhone}, Path=Title, Mode=TwoWay}" />
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Source={StaticResource myPhone}, Path=Price, Mode=TwoWay}" />


There are also buttons to display the current value of the object, as well as to change it:
<Button Content="Вывести текущие значения" Click="Button_Click_1" />
<Button Content="Изменить значение класса " Click="Button_Click_2" />

Click handlers:
private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Phone phone = (Phone)this.Resources["myPhone"];
            MessageBox.Show($"Модель: {phone.Title}; Стоимоть: {phone.Price}");
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Phone phone = (Phone)this.Resources["myPhone"];
            phone.Title = "iphone 6s";
            phone.Price = 50000;
        }


The result is such graphics (this is not the whole application, I test different things in it, but in order not to be distracted by them, I only provide directly related sections of the code)
61fc31cc3553b928180700.png

Further, I don’t know if it’s worth bringing screenshots, if necessary, send me a message. The bottom line is: it works half, well, that is, the binding works in one direction, but not in the other.

Destination -> Source
I was surprised, but yes it works. It does not apply any dependency properties. (Although if I understand correctly, then the dependency property is how you can probably translate the dependent property)

Source -> Receiver
Here's where it gets weird. This works when the program starts, but when the program is running, changes in the source (Title and Price properties) do not affect the destination properties (Text properties). At the same time, if I display the value of the class properties through a button click, then they really changed there. That is, it seems, on the contrary, a simpler connection, from the source to the receiver, for some reason, does not work.

First of all, I want to understand not how to fix it, but why the program behaves this way? The fact that the receiver changes the source is cool, there are no questions here, but why does the source not change the receiver? Namely, why he changes it only once, and therefore no, it is precisely this moment that is most unclear.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question