wpfpathstylesresourcedictionaryvisualbrush

WPF - Error inserting VisualBrush into a Style?


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>    


Solution

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