WPF按钮内容(矢量绘图)不可见

时间:2018-11-17 07:03:39

标签: wpf xaml

我在资源字典Buttons.xml中有两个矢量图,将它们显示为按钮内容。

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Viewbox
        x:Key="BlueMinusButton"
        Height="20"
        Stretch="Uniform">
        <Canvas
            Width="100"
            Height="100">
            <Canvas.LayoutTransform>
                <ScaleTransform
                    ScaleX="1"
                    ScaleY="-1"
                    CenterX=".5"
                    CenterY=".5" />
            </Canvas.LayoutTransform>
            <Ellipse
                Canvas.Left="0"
                Canvas.Top="0"
                Height="100"
                Width="100"
                Fill="Blue" />
            <Line
                X1="10"
                X2="90"
                Y1="50"
                Y2="50"
                Fill="Black"
                StrokeThickness="10"
                Stroke="Black" />
        </Canvas>
    </Viewbox>
    <Viewbox
        x:Key="GreenPlusButton"
        Height="20"
        Stretch="Uniform">
        <Canvas
            Width="100"
            Height="100">
            <Canvas.LayoutTransform>
                <ScaleTransform
                    ScaleX="1"
                    ScaleY="-1"
                    CenterX=".5"
                    CenterY=".5" />
            </Canvas.LayoutTransform>
            <Ellipse
                Canvas.Left="0"
                Canvas.Top="0"
                Height="100"
                Width="100"
                Fill="Green" />
            <Line
                X1="10"
                X2="90"
                Y1="50"
                Y2="50"
                Fill="Black"
                StrokeThickness="10"
                Stroke="Black" />
            <Line
                X1="50"
                X2="50"
                Y1="10"
                Y2="90"
                Fill="Black"
                StrokeThickness="10"
                Stroke="Black" />
        </Canvas>
    </Viewbox>
</ResourceDictionary>

view1.xaml中,我有三个按钮和其他控件,并且从资源中将相同的内容用于两个按钮。

<UserControl
    x:Class="ApplicationName.View1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:ApplicationName">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary
                    Source="Buttons.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition
                Width="*" />
            <ColumnDefinition
                Width="Auto" />
        </Grid.ColumnDefinitions>
        <Label
            Grid.Column="0"
            Grid.Row="0"
            Content="Object1" />
        <Button
            Grid.Column="1"
            Grid.Row="0"
            Content="{StaticResource BlueMinusButton}"
            Command="{Binding DoCommand1}" />
        <Label
            Grid.Column="0"
            Grid.Row="1"
            Content="Object2" />
        <Button
            Grid.Column="1"
            Grid.Row="1"
            Content="{StaticResource GreenPlusButton}"
            Command="{Binding DoCommand2}" />
        <Label
            Grid.Column="0"
            Grid.Row="2"
            Content="Object3" />
        <Button
            Grid.Column="1"
            Grid.Row="2"
            Content="{StaticResource GreenPlusButton}"
            Command="{Binding DoCommand3}" />
    </Grid>
</UserControl>

view1.xamlview2.xaml中使用。

<UserControl
    x:Class="ApplicationName.View2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="ApplicationName">
    <Grid>
        <StackPanel>
            <Label
                Content="Controls" />
            <local:View1 
                DataContext="{Binding View1Context}"/>
            <Label
                Content="Other controls" />
        </StackPanel>
    </Grid>
</UserControl>

问题来了:在xaml编辑器中查看view2.xaml时,缺少中间按钮的内容。运行时也会发生同样的情况。如果我在xaml编辑器中查看view1.xaml,则所有按钮内容均可见。这是视图的图片:

Views

为什么不显示内容以及如何找回?

1 个答案:

答案 0 :(得分:0)

如果要在多个地方使用x:Shared="False",请为每个ViewBox设置{{1}}。