WPF - 将VisualBrush插入样式时出错?

时间:2013-01-06 04:06:20

标签: wpf path styles resourcedictionary visualbrush

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

类型System.Windows.Media.VisualBrush的对象无法应用于需要类型Systems.Windows.Style的属性。

我怎样才能将所有路径保存在画布中并仍可在Style中检索?

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

<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>    

1 个答案:

答案 0 :(得分:2)

您的VisualBrush旨在分配给接受画笔的对象,例如背景而不是您尝试的样式。我建议你做一些这样的事情。(如果你需要能够选择你的画笔,那么这个例子会为每个VisualBrush分配相同的TextBox然后我会建议你您将x:Key添加回您的样式,并将样式分别指定给TextBox的

<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>