1

I need to keep all my paths in a canvas so I can retrieve them easily. I found out that it works with VisualBrush. When I try to retrieve the VisuahBrush and put in into Styles, it has error:

Object of the type System.Windows.Media.VisualBrush cannot be applied to a property that expects the type Systems.Windows.Style.

How else can I keep all my paths in a canvas and still retrievable in Styles?

Any ideas will be a great help, thanks!

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

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Lily S
  • 245
  • 1
  • 8
  • 18

1 Answers1

2

Your VisualBrush is designed to be assigned to an object that accepts a Brush, like a Background not a Style like you are attempting to. I would suggest that you do some thing like this.(This example will assign the same VisualBrush to every TextBox if you need to be able to pick and choose your brushes then I would suggest that you add back the x:Key to your style and assign the Styles individually to your TextBox's)

<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>
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • Hi Mark, thanks for your reply. Your example worked well, thanks for the explanation. When I change the `TargetType="{x:Type Path}` it says: Property 'background' was not found in type 'Path'. Can changing my Resources resolve this? Thanks! – Lily S Jan 07 '13 at 01:01
  • @LilyT what are you trying to accomplish? Your example in your question was using a TextBox, which has a BackGround Property. If you are using Paths, you have a Fill Property but you need to assign it with a key otherwise you will get a stackoverlow because the brush has a path also. I will edit the example. – Mark Hall Jan 07 '13 at 04:25
  • 1
    Thanks for the reply. I wanted to try if I could reuse the resource to a different source. Your example helped me understand better. Thanks once again! – Lily S Jan 08 '13 at 05:10