[英]Binding to LayoutAnchorableItem Visibility in AvalonDock 2
[英]How to bind Style property for LayoutAnchorableItem in XAML file with AvalonDock?
我在项目中创建了一个avalon停靠接口,例如,我想与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>
非常感谢 !
如果要在多个样式之间进行选择,则对接管理器上有一个使用样式选择器的LayoutItemContainerStyleSelector属性。 使用此样式选择器,可以根据对象是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}" ...
您可以省略样式选择器并删除资源中的键。 请注意,这些样式随后将应用于所有子项,这通常不是您想要的。
感谢您的回答! 我不确定它是否可以解决我的问题...我想要的是为其他目标类型定义一种样式,例如是否可以编写:
<Style TargetType="{x:Type dockctrl:LayoutItem}">
[...]
</Style>
<Style TargetType="{x:Type dockctrl:LayoutAnchorableItem}">
[...]
</Style>
但是我无法将这两种样式都直接写入DockingManager.LayoutItemContainerStyle分支中。 它仅接受一个样式定义...该如何处理? 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.