Mandatory data binding UserControl "IsEnabled" does not work

I have some terrible problem with my WPF application right now ...

I have a custom UserControl used to edit component details. It should begin with what is not turned on and becomes turned on as soon as the user selects a component for editing.

The problem is that the IsEnabled property does not even change.

Here is my code:

<my:UcComponentEditor Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsEnabled="{Binding EditorEnabled}" DataContext="{Binding VmComponent}" /> 

EditorEnabled is a property in my ViewModel (VmComponent) and defaults to false, becomes true when the user selects a component or creates it

For write only in my ViewModel:

 private Boolean _editorEnabled = false; public Boolean EditorEnabled { get { return _editorEnabled; } set { _editorEnabled = value; OnPropertyChanged("EditorEnabled"); } } 

When I try to start the application, UserControl is activated .... I have added breakpoints everywhere, EditorEnabled is false from the very beginning.

I also did a terribly stupid thing to try to figure out what was going on: I created a converter (so useful - converting the logical to boolean - eh), set a breakpoint on it and ... The code never reached.

 <my:UcComponentEditor Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsEnabled="{Binding EditorEnabled, Converter={StaticResource BoolConverter}}" DataContext="{Binding VmComponent}" /> 

This probably means that the isEnabled property is never set since the converter was never reached.

Do you see any problem there? I started working in WPF about a week ago, and so I might have missed something important ...

Thank you very much for your time :-)

+4
source share
3 answers

You must add DependencyProperty for the binding to work properly. See here for more details .

Code for:

 public static readonly DependencyProperty EditorEnabledDependencyProperty = DependencyProperty.Register("EditorEnabled", typeof(bool), typeof(UcComponentEditor), new PropertyMetadata(false)); public bool EditorEnabled { get { return (bool)base.GetValue(UcComponentEditor.EditorEnabledDependencyProperty); } set { base.SetValue(UcComponentEditor.EditorEnabledDependencyProperty, value); } } 
+2
source

I think the problem is with the DataContext attribute of the user control. This means that the EditorEnabled property must be a property of the VmComponent object. At least that was my problem.

To get around this, I pointed out the correct IsEnabled binding source. As soon as I did this, the control began to work as expected.

Hope this helps.

+1
source

Encapsulating your control in a DockPanel (for example) will eliminate the need for DependencyProperty.

Then you can simply bind to the dock panel instead of the user control. Setting a variable bound to IsEnabled in the Dock automatically enables or disables elements contained in the Dockpanel.

0
source

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


All Articles