如何使用AvalonDock将XAML文件中的LayoutAnchorableItem的Style属性绑定?

问题描述 投票:0回答:2

我在我的项目中创建了一个avalon dock接口,我想与LayoutAnchorableItem属性“Visibility”进行交互,但是如何将它实现到我的XAML代码中?我的DockingManager.LayoutItemContainerStyle分支中没有两个样式定义...

我要添加的行:

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

我原来的XAML代码:

<dock:DockingManager DataContext="{Binding DockManagerViewModel}" DocumentsSource="{Binding Documents}" AnchorablesSource="{Binding Anchorables}" >
    <dock:DockingManager.Resources>
    <!-- add views for specific ViewModels -->
        <DataTemplate DataType="{x:Type vmdock:SampleDockWindowViewModel}">
            <uscontrol:SampleDockWindowView />
        </DataTemplate>
    </dock:DockingManager.Resources>
    <dock:DockingManager.LayoutItemContainerStyle>
    <!--you can add additional bindings from the layoutitem to the DockWindowViewModel-->
        <Style TargetType="{x:Type dockctrl:LayoutItem}">
            <Setter Property="Title" Value="{Binding Model.Title}" />
            <Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}" />
            <Setter Property="CanClose" Value="{Binding Model.CanClose}" />
            <Setter Property="IsSelected" Value="{Binding Model.IsSelected}" />
        </Style>
    </dock:DockingManager.LayoutItemContainerStyle>

非常感谢 !

c# wpf avalondock
2个回答
1
投票

如果你想在多个样式之间进行选择,在dockmanager上有一个使用LayoutItemContainerStyleSelectorStyle Selector属性。使用此样式选择器,您可以根据对象是LayoutAnchorableItem还是不同类型的LayoutItem来选择要应用的样式。

public class MyStyleSelector : StyleSelector
{

    public Style DefaultStyle { get; set; }
    public Style CustomStyle { get; set; }

    public override Style SelectStyle(object item, DependencyObject container)
    {
        if (item is LayoutAnchorableItem)
        {
            return CustomStyle;
        }
        return DefaultStyle;
    }

}

如果要将单个样式设置器合并到另一个样式,可以使用BasedOn属性。这是有效的,因为LayoutAnchorableItem继承了LayoutItem。有了这个,你可以根据另一种风格制作一种风格,这样它就可以继承所有的设定者。资源看起来像:

<Style TargetType="{x:Type dockctrl:LayoutItem}" x:Key="DefaultStyle">
  <Setter Property="Title" Value="{Binding Model.Title}" />
  <Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}" />
  <Setter Property="CanClose" Value="{Binding Model.CanClose}" />
  <Setter Property="IsSelected" Value="{Binding Model.IsSelected}" />
</Style>

<Style TargetType="{x:Type dockctrl:LayoutAnchorableItem}" BasedOn="{StaticResource DefaultStyle}" x:Key="CustomStyle">
  <Setter Property="Visibility" Value="{Binding Model.IsVisible, ConverterParameter={x:Static Visibility.Hidden}, Converter={StaticResource btvc}, Mode=TwoWay}" />
</Style>

<local:MyStyleSelector DefaultStyle="{StaticResource DefaultStyle}" CustomStyle="{StaticResource CustomStyle}" x:Key="MyStyleSelector" />

现在,您可以使用新样式选择器填充停靠管理器。

<dock:DockingManager LayoutItemContainerStyleSelector="{StaticResource MyStyleSelector}" ...

您可以省略样式选择器并删除资源中的键。请注意,这些样式将应用于所有孩子,这不是您通常想要的。


0
投票

感谢您的回答!我不确定它是否处理我的问题......我想要的是为其他目标类型定义一种样式,就像我可以写的:

<Style TargetType="{x:Type dockctrl:LayoutItem}">
[...]
</Style>

<Style TargetType="{x:Type dockctrl:LayoutAnchorableItem}">
  [...]
</Style>

但是我无法将这两种样式直接写入我的DockingManager.LayoutItemContainerStyle分支。它只接受一个样式定义......如何处理它?谢谢

© www.soinside.com 2019 - 2024. All rights reserved.