Question

am probably doing something but can't figure out my problem. Any help would be really appreciated:

I am having a CustomControl called Section. A Section is collapsible and therefore its ControlTemplate contains an Expander. The Expanders IsExpanded-Property is bound to the Section's IsExpanded Property via TemplateBinding.

When setting IsExpanded on a Section the Expander collapses, but using the toggleButton within the Expander appears to break that binding. Probably by setting a local value to the Expander's IsExpanded-Property. Anyways, after changing the Expander state via Mouse the binding breaks and setting the Section's IsExpanded doesn't do anything.

This however does not happen when putting an Expander into a view and binding its IsExpanded-Property to some DP in the View.

Also notworthy: Snoop does not show any Bindings on the Expander's IsExpanded-Property. It only shows the Value-Source is ParentTemplate. As soon as I click the ToggleButton to Change IsExpanded the Value-Source changes to Local (possibly breaking the former Binding?)

Section.cs:

public class Section : Control
{
    static Section()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Section), new FrameworkPropertyMetadata(typeof(Section))); 
    }

    public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register(
            "IsExpanded", typeof (bool), typeof (Section), new PropertyMetadata(default(bool)));

    public bool IsExpanded
    {
        get { return (bool) GetValue(IsExpandedProperty); }
        set { SetValue(IsExpandedProperty, value); }
    }
}

Generic.xaml Style:

<Style TargetType="{x:Type local:Section}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:Section}">
                    <Expander Header="Test" IsExpanded="{TemplateBinding IsExpanded}" >
                        <Rectangle Fill="Aqua" Height="200" Width="200" />
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Any ideas?

Was it helpful?

Solution

So I found the answer which is pretty basic knowledge actually:

TemplateBindings are always ONEWAY no matter what the MetaData states...

Using:

IsExpanded="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsExpanded}"

fixed my problem...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top