【问题标题】:WPF - Bind vector image to Enhanced TooltipWPF - 将矢量图像绑定到增强的工具提示
【发布时间】:2019-03-10 05:24:36
【问题描述】:

在我的 MainWindow 中,我有一个带有一系列按钮的工具栏。 每个按钮显示一个图标,该图标是从 Inkscape 转换而来的矢量图像,格式如下:

<Viewbox>
    <Canvas>
        <Geometry .../>
    </Canvas>
</Viewbox>

所有图标都存储在 ResourceDictionary 中:

<DataTemplate x:Key="myIcon">
    <Viewbox>
        <Canvas>
            <Geometry .../>
        </Canvas>
    </Viewbox>
</DataTemplate>

并通过 ContentPresenter 分配给相关按钮:

<Button x:Name="myButton">
    <Button.ToolTip>
        <myDependencyProperties:EnhancedTooltip Header="Main" Style="{StaticResource EnhancedTooltip}" Content="Main Page"/>
    </Button.ToolTip>
    <Button.Content>
        <ContentPresenter ContentTemplate="{StaticResource myIcon}"/>
    </Button.Content>
</Button>

我的增强工具提示控件应显示父按钮的图标(与 MS Office 功能区中的某些按钮一样)。 我尝试了几种方法来绑定工具提示的内容,添加标签或按钮并将其内容绑定到按钮之一,例如:

<ContentPresenter ContentTemplate="{Binding Path=ManyDifferentAttempts, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>

不用说我失败了。 我还尝试通过样式将图标分配给按钮,如下所示:

<Button x:Name="myButton">
    <Button.ToolTip>
        <myDependencyProperties:EnhancedTooltip Header="Main" Style="{StaticResource EnhancedTooltip}" Content="Main Page"/>
    </Button.ToolTip>
    <ContentControl Style="{StaticResource myIcon}"/>
</Button>

图标当然存储为样式:

<Style x:Key="myIcon" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Viewbox>
                    <Canvas>
                        <Geometry .../>
                    </Canvas>
                </Viewbox>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

并将内容控件(标签或按钮)添加到增强工具提示,通过将其 ContentPresenter 样式绑定到父按钮之一来模仿父按钮行为。 没有任何成功。

当我使用图像 (png) 作为按钮的图标时,我可以轻松实现目标:

<Button x:Name="myButton">
    <Button.ToolTip>
        <myDependencyProperties:EnhancedTooltip Header="Main" Style="{StaticResource EnhancedTooltip}" Content="Main Page"/>
    </Button.ToolTip>
    <Image Source="PathToMyPNGImage"/>
</Button>

并且,在我的增强工具提示样式中:

<Image Source="{Binding Path=Content.Source, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}} }"/>

既然我决定改用矢量图像,我该如何获得相同的结果? 我可以很容易地回到以前的方法,但我想学习一些新东西。

感谢您的宝贵时间。

马可

【问题讨论】:

    标签: c# wpf binding


    【解决方案1】:

    您不能只显示几何图形。

    使用路径。

    将其数据设置或绑定到您的几何图形之一。

    丢失视图框 - 可能还有画布。如果您有内容控件,请制作路径内容。

    设置拉伸以填充路径。

    如果这是一个常见要求并且是几何图形的附加属性,我希望有一个可重复使用的模板。但这是让您了解我的想法的快速而肮脏的版本。

        <Button x:Name="myButton"
                Tag="{StaticResource Star}"
                >
            <Button.Content>
                <Path Fill="Blue"
                      Data="{Binding Tag, RelativeSource={RelativeSource AncestorType=Button}}"/>
            </Button.Content>
            <Button.ToolTip>
                <ToolTip>
                    <Path Fill="Blue"
                          Data="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"/>
                </ToolTip>
            </Button.ToolTip>
        </Button>
    

    【讨论】:

    • 安迪,谢谢你的评论,很抱歉回复晚了(我一直在忙工作)。我试图听从您的建议,但我不知道如何将用作按钮内容的矢量图像(无论是画布还是几何图形)实际绑定到工具提示内容。你能澄清一下吗?
    • 安迪,再次感谢您抽出宝贵时间。我使用了您的想法,即使用我的矢量图像的数据将 Button 的 Tag 属性设置为等于 StaticResource(无需将其作为 Path,我可以将其保留为 Canvas)。然后,我将 Tooltip 的 ContentPresenter 的 ContentTemplate 绑定到了按钮类型祖先的 Tag 属性。它工作得很好。为你的想法点赞。
    猜你喜欢
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多