Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UserControl "IsEnabled" data binding not working

I have a kinda awful problem with my WPF application right now...

I have a custom UserControl used to edit details of a component. It should start by being not enabled, and become enabled as soon as the user chose a component to edit.

The problem is: 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 is by default false, becomes true when the user chose a component or created one

Just for the record, in my ViewModel:

private Boolean _editorEnabled = false;

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

When I try to launch my app, the UserControl is starting... enabled. I added breakpoints everywhere, the EditorEnabled is false from the beginning.

I also did a horribly stupid thing to try to figure out what's happening: I created a converter (so useful -- converting a boolean to boolean -- eh), put a breakpoint on it, and... The code is never reached.

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

That probably means that the property isEnabled is never set, since the converter is never reached.

Do you see any kind of problem there? I started working in WPF about one week ago and therefore I may have missed something essential...

Thank you very much for your time :-)

like image 287
Damascus Avatar asked Oct 12 '22 12:10

Damascus


2 Answers

You should add a DependencyProperty for the binding to work properly. See here for more information.

Code-behind:

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); }
}
like image 186
Josh M. Avatar answered Oct 14 '22 08:10

Josh M.


The issue I think is that there is a binding on the DataContext property of the user control. Which means the EditorEnabled property should be a property in the VmComponent object. At least that's what my problem was.

To get around it, I specified a proper source to the binding of IsEnabled. Once I did that the control started working as expected.

Hope that helps.

like image 37
Kiran Avatar answered Oct 14 '22 09:10

Kiran