简体   繁体   中英

AvalonDock - Bind MenuItem to State of DockableContent

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.

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.
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM