Question

I'm puzzled by this probably trivial matter. I have my custom property on a descendant of DevExpresses class LayoutGroup (shouldn't make any difference IMO):

public class ExpandableLayoutGroup : LayoutGroup
{
    public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(Boolean), typeof(ExpandableLayoutGroup));

    public Boolean IsExpanded
    {
        get { return (Boolean) GetValue(IsExpandedProperty); }
        set
        {
            expandCheckBox.IsChecked = value;
            SetValue(IsExpandedProperty, value);
        }
    }
}

Then I have XAML binding to a listbox (containing 2 items at this time) called listStates.

<ExpandableLayoutGroup x:Name="groupTool" IsExpanded="{Binding SelectedValue.IsTool, ElementName=listStates}" DataContext="{Binding Tool}" Header="Tool" View="GroupBox" />

The object list binding to listStates contains both properties (simplified):

public class State
{
    public Boolean IsItemTool { get; private set; }
    public Tool Tool { get; private set; }
}

Am I missing something obvious? Because I would expect that when I change listbox selection (single) it would also update IsExpanded property on a group. I have more subproperties of Tool binded (as apparent by DataContext="{Binding Tool}") and those update well. So the DataContext is changing correctly on listbox selection. Except this one property IsExpanded (that should expand/collapse layoutgroup).

What am I doing wrong, and how to make it so, that when listbox changes, IsExpanded binding is polled to get value from IsTool, and will set it (depending on selection).

Était-ce utile?

La solution

Getters and setters of dependency properties must contain only GetValue and SetValue calls, because there're two ways to get and set their values: using getters and setters and using DependencyProperty or DependencyPropertyKey. This is why code in your setter is not executed (bindings don't use them for dependency properties). If you want some code to be executed when the value is changed, specify it as a PropertyChangedCallback callback in PropertyMetadata.

See PropertyChangedCallback Delegate on MSDN.

Autres conseils

The setter for a dependency property must only set the value for it's underlying type. The .NET internals reserve the right to call SetValue() directly (and in my experience WPF usually does, while Silverlight uses the setter that you've defined).

Refer to the "Implications for Custom Dependency Properties" section of XAML Loading and Dependency Properties for details

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top