【发布时间】:2014-09-22 18:57:03
【问题描述】:
我已经根据以下样式实现了一个 ToolBar 和样式。对于我使用 .xaml 资源文件中的矢量图形的图像
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Caliburn="http://www.caliburnproject.org">
<Rectangle x:Key="ToolBarButtonIcon"
x:Shared="False"
Visibility="{Binding IconVisibility}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Width="16" Height="16">
<Rectangle.Fill>
<VisualBrush Stretch="Uniform" Visual="{Binding IconSource}"/>
</Rectangle.Fill>
</Rectangle>
<Style x:Key="ToolBarToggleButton"
TargetType="{x:Type ToggleButton}"
BasedOn="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}">
<Setter Property="Content" Value="{StaticResource ToolBarButtonIcon}"/>
<Setter Property="ToolTip" Value="{Binding ToolTipText}"/>
<Setter Property="ToolTipService.IsEnabled" Value="{Binding ToolTipServiceEnabled}"/>
<Setter Property="IsChecked" Value="{Binding IsChecked}"/>
<Setter Property="Caliburn:Action.Target" Value="{Binding}"/>
<Setter Property="Caliburn:Message.Attach" Value="{Binding ActionText}"/>
</Style>
<Style x:Key="ToolBarButton"
TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<Setter Property="Content" Value="{StaticResource ToolBarButtonIcon}"/>
<Setter Property="ToolTip" Value="{Binding ToolTipText}"/>
<Setter Property="ToolTipService.IsEnabled" Value="{Binding ToolTipServiceEnabled}"/>
<Setter Property="Caliburn:Action.Target" Value="{Binding}"/>
<Setter Property="Caliburn:Message.Attach" Value="{Binding ActionText}"/>
</Style>
</ResourceDictionary>
这使工具栏变得很棒,看起来一切都很好
但如果我将一个工具栏组拖到另一个组上,图像就会消失,我会得到
我知道在图像源上使用了x:Shared="False",但在这种情况下这对我没有帮助。我该怎么做才能强制我的ToolBar 正确呈现?
注意。我还尝试在每个组件上设置所有 Virtualizing 属性,尽管我可能会影响这一点,但这并没有帮助。
感谢您的宝贵时间。
解决方案:
感谢@Mike Strobel,我有一个解决方案。它是改变每个按钮的样式来引用他们自己的Rectangle 图像容器。愚蠢地没有发现这一点,但最终样式变成了
<Style x:Key="ToolBarToggleButton"
TargetType="{x:Type ToggleButton}"
BasedOn="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Rectangle x:Shared="False"
Visibility="{Binding IconVisibility}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Width="16" Height="16">
<Rectangle.Fill>
<VisualBrush Stretch="Uniform" Visual="{Binding IconSource}"/>
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ToolTip" Value="{Binding ToolTipText}"/>
<Setter Property="ToolTipService.IsEnabled" Value="{Binding ToolTipServiceEnabled}"/>
<Setter Property="IsChecked" Value="{Binding IsChecked}"/>
<Setter Property="Caliburn:Action.Target" Value="{Binding}"/>
<Setter Property="Caliburn:Message.Attach" Value="{Binding ActionText}"/>
</Style>
<Style x:Key="ToolBarButton"
TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Rectangle x:Shared="False"
Visibility="{Binding IconVisibility}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Width="16" Height="16">
<Rectangle.Fill>
<VisualBrush Stretch="Uniform" Visual="{Binding IconSource}"/>
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ToolTip" Value="{Binding ToolTipText}"/>
<Setter Property="ToolTipService.IsEnabled" Value="{Binding ToolTipServiceEnabled}"/>
<Setter Property="Caliburn:Action.Target" Value="{Binding}"/>
<Setter Property="Caliburn:Message.Attach" Value="{Binding ActionText}"/>
</Style>
我希望这对其他人有帮助。
工具提示的其他绑定错误
我再次更改了样式以避免工具提示绑定在ToolBarItems 容器被拖动时也会失败。现在的问题是,工具提示仅在将鼠标悬停在包含图像的矩形上而不是在按钮表面上时才会弹出...
<Style x:Key="ToolBarButton"
TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border Background="Transparent"
ToolTip="{Binding FullToolTip}"
ToolTipService.IsEnabled="{Binding HasToolTip}">
<Rectangle x:Shared="False"
Visibility="{Binding IconVisibility}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Width="16" Height="16">
<Rectangle.Fill>
<VisualBrush Stretch="Uniform" Visual="{Binding IconSource}"/>
</Rectangle.Fill>
</Rectangle>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Caliburn:Action.Target" Value="{Binding}"/>
<Setter Property="Caliburn:Message.Attach" Value="{Binding ActionText}"/>
</Style>
【问题讨论】:
标签: c# wpf xaml rendering toolbar