M
M
Mozzarella2018-02-20 18:27:21
.NET
Mozzarella, 2018-02-20 18:27:21

How to set dependency property in WPF?

I have a custom component class containing a dependency property

public partial class FormulaEditor : UserControl
    {
        public static readonly DependencyProperty FormulaBindingProperty =
            DependencyProperty.Register("FormulaBinding", typeof(Formula), typeof(FormulaEditor));
        /// <summary>
        /// Свойство зависимости для привязки объекта формулы
        /// </summary>
        public Formula FormulaBinding
        {
            get
            {
                return GetValue(FormulaBindingProperty) as Formula;
                
            }
            set
            {
                SetValue(FormulaBindingProperty, value);
                ViewModel = new FormulaViewModel(value);
            }
        }
...
}

There is a ViewModel
public class WorkTypeViewModel : BaseViewModel<WorkTypeViewModel>
    {
        public int Id { get; set; }

        public string Name { get; set; }
        
        public Formula Formula { get; set; }
...
}

There is a window that uses binding and ViewModel
<local:FormulaEditor Margin="0" Grid.Row="2" MinWidth="500" MinHeight="250" FormulaBinding="{Binding Path=Formula}"/>

All other bindings through the DataContext in this window work, but even the get and set methods of the FormulaBinding dependency property are not called in the UserControl. It is necessary that when the FormulaBinding changes in the UserControl, the Formula in the ViewModel changes accordingly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Yudakov, 2018-02-20
@AlexanderYudakov

WPF does not use the "FormulaBinding" property setter and calls "SetValue()" directly.
You can track changes using the PropertyChangedCallback. Example here:
https://msdn.microsoft.com/en-us/library/system.wi...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question