1

我需要将所有路径保存在画布中,以便轻松检索它们。我发现它适用于VisualBrush. 当我尝试检索VisuahBrush并放入Styles时,它有错误:

该类型的对象System.Windows.Media.VisualBrush不能应用于需要该类型的属性Systems.Windows.Style

我还能如何将所有路径保存在画布中并且仍然可以在Styles 中检索?

任何想法都会有很大帮助,谢谢!

<VisualBrush x:Key="myVisualBrush">
    <VisualBrush.Visual>
        <Canvas>
            <Path Fill="#FF231F20" Stretch="Fill" Width="12.69" Height="14.477" Canvas.Left="652.196" Canvas.Top="88.61" Data="F1M617.2051,52.7275C616.7281,52.4525,616.3391,52.6775,616.3391,53.2275L616.3391,66.4695C616.3391,67.0195,616.7281,67.2445,617.2051,66.9695L628.6721,60.3485C629.1481,60.0735,629.1481,59.6235,628.6721,59.3485z"/>
        </Canvas>
    </VisualBrush.Visual>
</VisualBrush>

<Style x:Key="myVisualStyle" BasedOn="{StaticResource myVisualBrush}" TargetType="{x:Type TextBox}">
    <!-- Styles -->
</Style>    

4

1 回答 1

2

您的 VisualBrush 旨在分配给接受画笔的对象,例如背景而不是您尝试的样式。我建议你做这样的事情。(如果你需要能够挑选和选择你的画笔,这个例子将为VisualBrush每个人分配相同的东西,那么我建议你添加回你的样式并将样式单独分配给你的文本框的TextBoxx:Key

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="400" Width="400">
    <Window.Resources>
        <VisualBrush x:Key="myVisualBrush">
            <VisualBrush.Visual>
                <Canvas>
                    <Path Fill="#FF231F20" Stretch="Fill" Width="12.69" Height="14.477" Canvas.Left="652.196" Canvas.Top="88.61" Data="F1M617.2051,52.7275C616.7281,52.4525,616.3391,52.6775,616.3391,53.2275L616.3391,66.4695C616.3391,67.0195,616.7281,67.2445,617.2051,66.9695L628.6721,60.3485C629.1481,60.0735,629.1481,59.6235,628.6721,59.3485z"/>
                </Canvas>
            </VisualBrush.Visual>
        </VisualBrush>

        <Style  TargetType="{x:Type TextBox}">
            <Setter Property="Background" Value="{StaticResource myVisualBrush}"/>
        </Style>
        <Style x:Key="myPathStyle" TargetType="{x:Type Path}">
            <Setter Property="Fill" Value="{StaticResource myVisualBrush}"/>
        </Style>
    </Window.Resources>
    <Grid>
        <TextBox  x:Name="MyTextBox" />
        <Path Style="{StaticResource myPathStyle}"  x:Name="myPath" >
            <Path.Data>
                <RectangleGeometry Rect="100,100,100,100"/>
            </Path.Data>        
        </Path>
    </Grid>
</Window>
于 2013-01-06T04:33:34.157 回答