绑定到LayoutAnchorableItem能见度AvalonDock 2 [英] Binding to LayoutAnchorableItem Visibility in AvalonDock 2

查看:506
本文介绍了绑定到LayoutAnchorableItem能见度AvalonDock 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图 LayoutAnchorableItem 能见度绑定到视图模型一个布尔值,这样我可以以编程方式显示和隐藏anchorable:

I am attempting to bind the Visibility of LayoutAnchorableItem to a boolean in the ViewModel so that I can programmatically show and hide the anchorable:

<UserControl.Resources>
    <avalon:BoolToVisibilityConverter x:Key="btvc"/>
</UserControl.Resources>

<avalon:DockingManager>
    <avalon:DockingManager.LayoutItemContainerStyleSelector>
        <ws:WorkspaceStyleSelector>
            <ws:WorkspaceStyleSelector.AnchorableStyle>
                <Style TargetType="{x:Type avalon:LayoutAnchorableItem}">
                    <!-- ... -->
                    <Setter Property="Visibility" Value="{Binding Model.IsVisible, Converter={StaticResource btvc}, Mode=TwoWay}"/>
                </Style>
            </ws:WorkspaceStyleSelector.AnchorableStyle>
        </ws:WorkspaceStyleSelector>
    </avalon:DockingManager.LayoutItemContainerStyleSelector>

    <!-- ... -->

</avalon:DockingManager>



然而,每当我隐藏anchorable,抛出一个异常:对象引用没有被设置为一个实例

at Xceed.Wpf.AvalonDock.Layout.LayoutContent.Close() in ...\Xceed.Wpf.AvalonDock\Layout\LayoutContent.cs:line 346
at Xceed.Wpf.AvalonDock.Controls.LayoutItem.OnVisibilityChanged() in ...\Xceed.Wpf.AvalonDock\Controls\LayoutItem.cs:line 310
at Xceed.Wpf.AvalonDock.Controls.LayoutAnchorableItem.OnVisibilityChanged() in ...\Xceed.Wpf.AvalonDock\Controls\LayoutAnchorableItem.cs:line 299
at Xceed.Wpf.AvalonDock.Controls.LayoutItem.OnVisibilityChanged(DependencyObject s, DependencyPropertyChangedEventArgs e) in ...\Xceed.Wpf.AvalonDock\Controls\LayoutItem.cs:line 303
at Xceed.Wpf.AvalonDock.Controls.LayoutItem.<.cctor>b__1(DependencyObject s, DependencyPropertyChangedEventArgs e) in ...\Xceed.Wpf.AvalonDock\Controls\LayoutItem.cs:line 37
at System.Windows.PropertyChangedCallback.Invoke(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
...


$
b $ b

注释掉结合知名度后,如预期的那样anchorable是隐藏的。

After commenting out the binding to visibility, the anchorable is hidden as expected.

推荐答案

您需要添加一个 ConverterParameter 的价值 Visibility.Hidden 来绑定:

tl;dr

You need to add a ConverterParameter of value Visibility.Hidden to the Binding:

<Setter Property="Visibility" Value="{Binding Model.IsVisible, ConverterParameter={x:Static Visibility.Hidden}, Converter={StaticResource btvc}, Mode=TwoWay}"/>

该转换器参数是能见度即返回时,布尔是隐藏表示anchorable是隐藏的。

The converter parameter is the Visibility that is returned when the boolean is false, and Hidden means the anchorable is hidden.

如果我们看看 LayoutContent.Close(),它标有注释:

If we look at LayoutContent.Close(), it is marked with the comment:

请注意,通常anchorable只隐藏(不封闭)。默认情况下,当用户单击X按钮隐藏的仅仅是内容。

Please note that usually the anchorable is only hidden (not closed). By default when user click the X button it only hides the content.

所以这不应该被调用。纵观堆栈跟踪,这是从名为:

So this should not have been called. Looking at the stacktrace, this is called from:

// LayoutItem class.
protected virtual void OnVisibilityChanged()
{
    if (LayoutElement != null &&
        Visibility == System.Windows.Visibility.Collapsed)
        LayoutElement.Close();
}



据微软称, System.Windows.Visibility。折叠表示,该项目是不可见的,布局过程中空间不保留它。这听起来像什么与anchorables发生的事情,当我们点击 X 来隐藏他们(这大概是什么地方发生了可视化树)。既然这样,为什么评论说,这是不正常呼吁anchorables?如果我们看看 LayoutAnchorableItem.OnVisibilityChanged()

According to Microsoft, System.Windows.Visibility.Collapsed means that the item is not visible and space is not reserved for it during layout. This sounds like what is happening with the anchorables when we click the X to hide them (and this is probably happening somewhere up the visual tree). But then why do the comments say that this is not normally called for anchorables? If we look at LayoutAnchorableItem.OnVisibilityChanged():

protected override void OnVisibilityChanged()
{
    if (_anchorable != null && _anchorable.Root != null)
    {
        if (_visibilityReentrantFlag.CanEnter)
        {
            using (_visibilityReentrantFlag.Enter())
            {
                if (Visibility == System.Windows.Visibility.Hidden)
                    _anchorable.Hide(false);
                else if (Visibility == System.Windows.Visibility.Visible)
                    _anchorable.Show();
            }
        }
    }

    base.OnVisibilityChanged();
}



很显然,AvalonDock使用 Visibility.Hidden 值,表明anchorable是隐藏的。 (这是一个有点混乱给我,因为微软表示隐藏隐藏在这不是anchorable的行为方式,当你隐藏它布局中的元素,但保留空间。)那么,为什么在能见度折叠而不是隐藏?答案就在 BoolToVisibilityConverter

it is clear that AvalonDock uses the Visibility.Hidden value to indicate that the anchorable is hidden. (This was a little confusing to me since Microsoft states that Hidden hides the element but reserves space in the layout which is not how the anchorable behaves when you hide it.) So why is the Visibility Collapsed instead of Hidden? The answer lies in the BoolToVisibilityConverter.

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (value is bool && targetType == typeof(Visibility))
    {
        bool val = (bool)value;
        if (val)
            return Visibility.Visible;
        else
            if (parameter != null && parameter is Visibility)
                return parameter;
            else
                return Visibility.Collapsed;
    }

    // ...
}

除非类型的参数<传递code>能见度, Visibility.Collapsed 当布尔是假的使用。我们希望假以平均 Visibility.Hidden ,所以我们设置为 ConverterParameter

Unless a parameter of type Visibility is passed, Visibility.Collapsed is used when the boolean is false. We want false to mean Visibility.Hidden so we set that as the ConverterParameter

<Setter Property="Visibility" Value="{Binding Model.IsVisible, ConverterParameter={x:Static Visibility.Hidden}, Converter={StaticResource btvc}, Mode=TwoWay}"/>

这篇关于绑定到LayoutAnchorableItem能见度AvalonDock 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆