Synchronizing Bindings of Multiple Properties in UserControl

I have an ugly race state with WPF user control, which is kind of extended ComboBox: UserControl basically defines two DependencyProperties to associate, one of which is the selected item, the other is a list from which the selected item can be selected. Both are connected, so the control can be initialized with or without the selected element, and both properties can be changed by binding (when changing the DataContext), in the future, the choice may change due to user interaction. UserControl contains a ComboBox whose ItemsSource and SelectedItem are in sync with my property list and SelectedItem UserControl - so far so good. The problem isthat if both properties change (quasi at the same time) externally when installing a new DataContext with the values ​​set, it sometimes happens that the SelectedItem is set correctly, but updating the list causes the reset option to be zero rewriting the previously set value → damage my DataContext.

To do this briefly: I need to find a way to “block” my SelectedItem while updating the list, but just watching PropertyChanged-Events is not enough, since I get them AFTER updates, where I have already lost the state that I need to remember. Also, I cannot identify if the selection change was caused by the user or (correctly) binding or (not desirable) indirectly by another binding ... I think I will need some BeforePropertyChanged or OnPropertyChanging event for my DependencyProperties - or another way to control streamlining simultaneous updates of both properties.

Any suggestions are welcome :)

Note that I'm talking about a list to select an item, but it's actually a more complex structure that allows you to quickly sort and filter, which is also the reason why I don't use ItemsControl here, but I don’t feel that it matters for the question.

+3
source share
1 answer

This may not help the situation and is probably not suitable for this, however you talked about the event OnPropertyChangingfor your dependency properties.

, PropertyMetadata, , , EventArgument.

Text

public static DependencyProperty TextProperty = DependencyProperty.Register
                                                ("Text", typeof(string), 
                                                 typeof(DecimalTextBox), 
                                                 new PropertyMetadata("", OnTextPropertyChanged));

- , . PropertyMetadata . - , propertychanged, , .

, , .

private static void OnTextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {

        var box = ((TextBox)sender);

        if (((string)e.NewValue)==badvalue)
                box.Text= e.OldValue);


    }

, , , , , null . (, , NULL, ItemSource, itemssource [ - ItemsSource, reset ]). , - .

u_u

+1

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


All Articles