WPF工具栏项目渲染失败

时间:2014-09-22 18:57:03

标签: c# wpf xaml rendering toolbar

我已根据以下样式实现了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>

这使得ToolBar变得很棒并且看起来很好看


Okay

但是,如果我将一个ToolBar组拖到另一个上,图像就会消失,我就会


Error

我知道在图像源上使用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>

我希望这有助于其他人。


ToolTip的其他绑定错误

我再次更改了样式,以避免在拖动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>

1 个答案:

答案 0 :(得分:1)

您的样式为每个按钮Rectangle分配相同的Content实例,但WPF不允许您在多个可视树中托管相同的视觉效果。尝试使用ContentTemplate来指定按钮的可视树。