views:

53

answers:

1

I'm using AvalonDock to layout my application.

I want to create a "View" MenuItem with a checkable MenuItem for each of my DockableContents that will show/hide each item.

I'm not finding an example of anyone doing this, and it appears to me the State property is readonly, making it not possible to create a 2-way binding to the MenuItem. It also looks like you have to call methods to change the State.

Anyone have a clever way to do this with bindings? Or is there a simple way to do it I'm missing.

+1  A: 

One possible solution is to use an attached property. The attached property would call the necessary methods to change the state. You could then bind to that.

public static class ContentAttach
{
    public static readonly DependencyProperty StateProperty = DependencyProperty.RegisterAttached(
        "State", typeof(DockableContentState), typeof(ContentAttach), new PropertyMetadata(StateChanged));
    public static void SetState(DockableContent element, DockableContentState value)
    {
        element.SetValue(StateProperty, value);
    }
    public static DockableContentState GetState(DockableContent element)
    {
        return (DockableContentState)element.GetValue(StateProperty);
    }
    private static void StateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var element = (DockableContent)d;
        var state = (DockableContentState)e.NewValue;
        switch (state)
        {
            // Call methods in here to change State.
        }
    }
}
Joseph Sturtevant
Good start. How would you get a reference to the specified Avalon DockableContent from within this static class? And I'd have to make it 2-way somehow, so I'd have to register for an event on the DockableContent and change the value of the attached DP.
Jonathan.Peppers
In the sample code, the specified DockableContent is the 'element' variable in StateChanged.
Joseph Sturtevant
Sorry, I was thinking you'd set this property on MenuItem, but you'd really just bind to the DockableContent's property and it would get passed in. I will try this.
Jonathan.Peppers