2

我添加了如下的 xaml 图形代码

<Viewbox x:key="Icon">
    <Canvas Name="Layer_1" Width="200" Height="200" Canvas.Left="0" Canvas.Top="0">
            <Canvas.RenderTransform>
                    <TranslateTransform X="0" Y="0"/>
            </Canvas.RenderTransform>
            <Canvas.Resources/>
            <!--Unknown tag: metadata-->
            <!--Unknown tag: sodipodi:namedview-->
            <Canvas Name="g3">
                    <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path5" Fill="#000000">
                            <Path.Data>
            <PathGeometry Figures="M56.575 97l-9.064-21.467L37.18 51.066L26.847 75.209L17.765 97H11.99c2.322 7 12.895 14.08 25.602 14.08   S60.872 104 63.193 97H56.575z M27.767 75.602l9.407-21.973l9.415 22.292L55.491 97H18.848L27.767 75.602z" FillRule="NonZero"/>
                            </Path.Data>
                    </Path>
                    <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path7" Fill="#000000">
                            <Path.Data>
            <PathGeometry Figures="M181.87 97l-9.065-21.467l-10.332-24.467l-10.332 24.143L143.059 97h-5.773c2.321 7 12.894 14.08 25.602 14.08   c12.707 0 23.279-7.08 25.602-14.08H181.87z M153.061 75.602l9.406-21.973l9.416 22.292L180.786 97h-36.646L153.061 75.602z" FillRule="NonZero"/>
                            </Path.Data>
                    </Path>
                    <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path9" Fill="#000000">
                            <Path.Data>
            <PathGeometry Figures="G169 42h-60.375c-0.968-3.1-3.476-5.511-6.625-6.363V14c0-1.381-1.119-2.5-2.5-2.5S97 12.619 97 14v21.638   c-3.148 0.853-5.656 3.264-6.625 6.362H30c-1.381 0-2.5 1.119-2.5 2.5S28.619 47 30 47h60.191c0.788 3.436 3.424 6.156 6.809 7.072   v102.682l-48 13.465V190h101v-19.781l-48-13.465V54.072c3.385-0.915 6.021-3.637 6.81-7.072J169c1.381 0 2.5-1.119 2.5-2.5   S170.381 42 169 42z G145 185H54v-10.992l45.5-12.762l45.5 12.762V185z M104.246 45.72c-0.016 0.085-0.027 0.173-0.048 0.257   c-0.046 0.199-0.11 0.391-0.18 0.58c-0.057 0.149-0.118 0.297-0.187 0.439c-0.408 0.842-1.048 1.541-1.844 2.023   c-0.285 0.173-0.594 0.311-0.913 0.425c-0.138 0.048-0.276 0.095-0.419 0.13c-0.106 0.027-0.213 0.051-0.321 0.07   c-0.272 0.048-0.548 0.084-0.834 0.084c-0.285 0-0.562-0.036-0.833-0.084c-0.109-0.02-0.217-0.043-0.323-0.07   c-0.142-0.035-0.278-0.082-0.415-0.129c-0.322-0.115-0.634-0.254-0.922-0.429c-0.792-0.481-1.429-1.179-1.836-2.017   c-0.07-0.145-0.132-0.293-0.189-0.443c-0.069-0.189-0.135-0.382-0.181-0.582c-0.02-0.083-0.03-0.168-0.046-0.253   c-0.047-0.268-0.083-0.539-0.083-0.82c0-0.3 0.036-0.592 0.089-0.877c0.025-0.137 0.066-0.269 0.104-0.399   c0.036-0.131 0.074-0.261 0.12-0.387c0.065-0.174 0.141-0.344 0.226-0.508c0.03-0.059 0.062-0.117 0.096-0.176   c0.109-0.191 0.226-0.379 0.358-0.555c0.367-0.484 0.819-0.895 1.34-1.211c0.289-0.176 0.602-0.315 0.925-0.431   c0.137-0.047 0.273-0.094 0.415-0.128c0.106-0.027 0.214-0.052 0.323-0.072c0.271-0.047 0.548-0.084 0.833-0.084   c0.286 0 0.562 0.037 0.834 0.084c0.108 0.021 0.216 0.044 0.321 0.071c0.143 0.035 0.281 0.082 0.419 0.13   c0.32 0.115 0.63 0.252 0.916 0.427c0.523 0.317 0.977 0.728 1.345 1.214c0.134 0.176 0.25 0.365 0.36 0.558   c0.031 0.057 0.063 0.112 0.093 0.171c0.086 0.165 0.161 0.336 0.227 0.512c0.047 0.125 0.084 0.254 0.119 0.383   c0.038 0.133 0.079 0.264 0.105 0.401c0.052 0.285 0.088 0.577 0.088 0.877C104.328 45.182 104.291 45.452 104.246 45.72y" FillRule="NonZero"/>
                            </Path.Data>
                    </Path>
            </Canvas>
    </Canvas>
</Viewbox>

它是一个在一个页面中重复使用的按钮。此代码位于 application.resources 中。我将它与静态资源一起使用。

    <Button Name="BarcodeButton" Content="{StaticResource Icon}"></Button>

当我单击添加按钮时,它会将这个新按钮添加到页面中。我发现的问题是它只显示最新的按钮。其他人都消失了。

编辑 我刚刚发现我必须 x:Shared="False"

<Viewbox x:key="Icon" x:Shared="False" >
    <Canvas Name="Layer_1" Width="200" Height="200" Canvas.Left="0" Canvas.Top="0">
            <Canvas.RenderTransform>
                    <TranslateTransform X="0" Y="0"/>
            </Canvas.RenderTransform>
            <Canvas.Resources/>
            <!--Unknown tag: metadata-->
            <!--Unknown tag: sodipodi:namedview-->
            <Canvas Name="g3">
                    <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path5" Fill="#000000">
                            <Path.Data>
            <PathGeometry Figures="M56.575 97l-9.064-21.467L37.18 51.066L26.847 75.209L17.765 97H11.99c2.322 7 12.895 14.08 25.602 14.08   S60.872 104 63.193 97H56.575z M27.767 75.602l9.407-21.973l9.415 22.292L55.491 97H18.848L27.767 75.602z" FillRule="NonZero"/>
                            </Path.Data>
                    </Path>
                    <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path7" Fill="#000000">
                            <Path.Data>
            <PathGeometry Figures="M181.87 97l-9.065-21.467l-10.332-24.467l-10.332 24.143L143.059 97h-5.773c2.321 7 12.894 14.08 25.602 14.08   c12.707 0 23.279-7.08 25.602-14.08H181.87z M153.061 75.602l9.406-21.973l9.416 22.292L180.786 97h-36.646L153.061 75.602z" FillRule="NonZero"/>
                            </Path.Data>
                    </Path>
                    <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path9" Fill="#000000">
                            <Path.Data>
            <PathGeometry Figures="G169 42h-60.375c-0.968-3.1-3.476-5.511-6.625-6.363V14c0-1.381-1.119-2.5-2.5-2.5S97 12.619 97 14v21.638   c-3.148 0.853-5.656 3.264-6.625 6.362H30c-1.381 0-2.5 1.119-2.5 2.5S28.619 47 30 47h60.191c0.788 3.436 3.424 6.156 6.809 7.072   v102.682l-48 13.465V190h101v-19.781l-48-13.465V54.072c3.385-0.915 6.021-3.637 6.81-7.072J169c1.381 0 2.5-1.119 2.5-2.5   S170.381 42 169 42z G145 185H54v-10.992l45.5-12.762l45.5 12.762V185z M104.246 45.72c-0.016 0.085-0.027 0.173-0.048 0.257   c-0.046 0.199-0.11 0.391-0.18 0.58c-0.057 0.149-0.118 0.297-0.187 0.439c-0.408 0.842-1.048 1.541-1.844 2.023   c-0.285 0.173-0.594 0.311-0.913 0.425c-0.138 0.048-0.276 0.095-0.419 0.13c-0.106 0.027-0.213 0.051-0.321 0.07   c-0.272 0.048-0.548 0.084-0.834 0.084c-0.285 0-0.562-0.036-0.833-0.084c-0.109-0.02-0.217-0.043-0.323-0.07   c-0.142-0.035-0.278-0.082-0.415-0.129c-0.322-0.115-0.634-0.254-0.922-0.429c-0.792-0.481-1.429-1.179-1.836-2.017   c-0.07-0.145-0.132-0.293-0.189-0.443c-0.069-0.189-0.135-0.382-0.181-0.582c-0.02-0.083-0.03-0.168-0.046-0.253   c-0.047-0.268-0.083-0.539-0.083-0.82c0-0.3 0.036-0.592 0.089-0.877c0.025-0.137 0.066-0.269 0.104-0.399   c0.036-0.131 0.074-0.261 0.12-0.387c0.065-0.174 0.141-0.344 0.226-0.508c0.03-0.059 0.062-0.117 0.096-0.176   c0.109-0.191 0.226-0.379 0.358-0.555c0.367-0.484 0.819-0.895 1.34-1.211c0.289-0.176 0.602-0.315 0.925-0.431   c0.137-0.047 0.273-0.094 0.415-0.128c0.106-0.027 0.214-0.052 0.323-0.072c0.271-0.047 0.548-0.084 0.833-0.084   c0.286 0 0.562 0.037 0.834 0.084c0.108 0.021 0.216 0.044 0.321 0.071c0.143 0.035 0.281 0.082 0.419 0.13   c0.32 0.115 0.63 0.252 0.916 0.427c0.523 0.317 0.977 0.728 1.345 1.214c0.134 0.176 0.25 0.365 0.36 0.558   c0.031 0.057 0.063 0.112 0.093 0.171c0.086 0.165 0.161 0.336 0.227 0.512c0.047 0.125 0.084 0.254 0.119 0.383   c0.038 0.133 0.079 0.264 0.105 0.401c0.052 0.285 0.088 0.577 0.088 0.877C104.328 45.182 104.291 45.452 104.246 45.72y" FillRule="NonZero"/>
                            </Path.Data>
                    </Path>
            </Canvas>
    </Canvas>
</Viewbox>

它工作正常。

4

2 回答 2

0

尝试使用按钮样式:

<Style x:Key="btnCustom" TargetType="{x:Type Button}">
            <Setter Property="Content">
                <Setter.Value>
                    <Viewbox>
                        <Canvas Name="Layer_1" Width="200" Height="200" Canvas.Left="0" Canvas.Top="0">
                            <Canvas.RenderTransform>
                                <TranslateTransform X="0" Y="0"/>
                            </Canvas.RenderTransform>
                            <Canvas.Resources/>
                            <!--Unknown tag: metadata-->
                            <!--Unknown tag: sodipodi:namedview-->
                            <Canvas Name="g3">
                                <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path5" Fill="#000000">
                                    <Path.Data>
                                        <PathGeometry Figures="M56.575 97l-9.064-21.467L37.18 51.066L26.847 75.209L17.765 97H11.99c2.322 7 12.895 14.08 25.602 14.08   S60.872 104 63.193 97H56.575z M27.767 75.602l9.407-21.973l9.415 22.292L55.491 97H18.848L27.767 75.602z" FillRule="NonZero"/>
                                    </Path.Data>
                                </Path>
                                <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path7" Fill="#000000">
                                    <Path.Data>
                                        <PathGeometry Figures="M181.87 97l-9.065-21.467l-10.332-24.467l-10.332 24.143L143.059 97h-5.773c2.321 7 12.894 14.08 25.602 14.08   c12.707 0 23.279-7.08 25.602-14.08H181.87z M153.061 75.602l9.406-21.973l9.416 22.292L180.786 97h-36.646L153.061 75.602z" FillRule="NonZero"/>
                                    </Path.Data>
                                </Path>
                                <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path9" Fill="#000000">
                                    <Path.Data>
                                        <PathGeometry Figures="G169 42h-60.375c-0.968-3.1-3.476-5.511-6.625-6.363V14c0-1.381-1.119-2.5-2.5-2.5S97 12.619 97 14v21.638   c-3.148 0.853-5.656 3.264-6.625 6.362H30c-1.381 0-2.5 1.119-2.5 2.5S28.619 47 30 47h60.191c0.788 3.436 3.424 6.156 6.809 7.072   v102.682l-48 13.465V190h101v-19.781l-48-13.465V54.072c3.385-0.915 6.021-3.637 6.81-7.072J169c1.381 0 2.5-1.119 2.5-2.5   S170.381 42 169 42z G145 185H54v-10.992l45.5-12.762l45.5 12.762V185z M104.246 45.72c-0.016 0.085-0.027 0.173-0.048 0.257   c-0.046 0.199-0.11 0.391-0.18 0.58c-0.057 0.149-0.118 0.297-0.187 0.439c-0.408 0.842-1.048 1.541-1.844 2.023   c-0.285 0.173-0.594 0.311-0.913 0.425c-0.138 0.048-0.276 0.095-0.419 0.13c-0.106 0.027-0.213 0.051-0.321 0.07   c-0.272 0.048-0.548 0.084-0.834 0.084c-0.285 0-0.562-0.036-0.833-0.084c-0.109-0.02-0.217-0.043-0.323-0.07   c-0.142-0.035-0.278-0.082-0.415-0.129c-0.322-0.115-0.634-0.254-0.922-0.429c-0.792-0.481-1.429-1.179-1.836-2.017   c-0.07-0.145-0.132-0.293-0.189-0.443c-0.069-0.189-0.135-0.382-0.181-0.582c-0.02-0.083-0.03-0.168-0.046-0.253   c-0.047-0.268-0.083-0.539-0.083-0.82c0-0.3 0.036-0.592 0.089-0.877c0.025-0.137 0.066-0.269 0.104-0.399   c0.036-0.131 0.074-0.261 0.12-0.387c0.065-0.174 0.141-0.344 0.226-0.508c0.03-0.059 0.062-0.117 0.096-0.176   c0.109-0.191 0.226-0.379 0.358-0.555c0.367-0.484 0.819-0.895 1.34-1.211c0.289-0.176 0.602-0.315 0.925-0.431   c0.137-0.047 0.273-0.094 0.415-0.128c0.106-0.027 0.214-0.052 0.323-0.072c0.271-0.047 0.548-0.084 0.833-0.084   c0.286 0 0.562 0.037 0.834 0.084c0.108 0.021 0.216 0.044 0.321 0.071c0.143 0.035 0.281 0.082 0.419 0.13   c0.32 0.115 0.63 0.252 0.916 0.427c0.523 0.317 0.977 0.728 1.345 1.214c0.134 0.176 0.25 0.365 0.36 0.558   c0.031 0.057 0.063 0.112 0.093 0.171c0.086 0.165 0.161 0.336 0.227 0.512c0.047 0.125 0.084 0.254 0.119 0.383   c0.038 0.133 0.079 0.264 0.105 0.401c0.052 0.285 0.088 0.577 0.088 0.877C104.328 45.182 104.291 45.452 104.246 45.72y" FillRule="NonZero"/>
                                    </Path.Data>
                                </Path>
                            </Canvas>
                        </Canvas>
                    </Viewbox>
                </Setter.Value>
            </Setter>
        </Style>

为您的按钮使用样式:

<Button Style="{StaticResource btnCustom}"/>

使用这种样式,我有这样的按钮: 在此处输入图像描述

我希望这是示例对您有帮助)))

于 2013-11-06T11:35:06.043 回答
0

这是因为您将内容设置为静态资源。您必须将样式或模板设置为静态资源。

<ControlTemplate x:Key="Icon">
    <Button>
        <Canvas>
            ...
        </Canvas>
    </Button>
</ControlTemplate>

<Button Name="BarcodeButton" Template="{StaticResource Icon}"></Button>
于 2013-11-06T11:05:13.493 回答