简体   繁体   English

AvalonDock-将MenuItem绑定到DockableContent的状态

[英]AvalonDock - Bind MenuItem to State of DockableContent

I'm using AvalonDock to layout my application. 我正在使用AvalonDock来布局我的应用程序。

I want to create a "View" MenuItem with a checkable MenuItem for each of my DockableContents that will show/hide each item. 我想为将显示/隐藏每个项目的每个DockableContents创建一个带有可检查MenuItem的“查看”菜单项。

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. 我找不到任何执行此操作的示例,在我看来State属性是只读的,因此无法创建与MenuItem的2向绑定。 It also looks like you have to call methods to change the State. 看起来您还必须调用方法来更改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.
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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