Reputation: 1379
So here's what's happening, I can set the background of an object to a gradient with either putting the brush directly into Grid.Background
or setting (I had thought that worked, but I just tested it and it also crashes the app), but setting it through a Setter (Background="{StaticResource BackgroundGradient}"
<Setter Property="Background" Value="{StaticResource BackgroundGradient}">
) crashes the app.
Setting it through a Setter worked the first time, then I closed the app and copy/pasted the Gradient a few times and changed the name and colours to make the other gradients I needed and after that it crashed every time a Setter sets the gradient.
Here is the relevant code from the Style object:
<Style x:Key="MainBackground" TargetType="Panel">
<Setter Property="Background" Value="{StaticResource BackgroundGradient}"/>
</Style>
Here is the corresponding gradient (generated by making the gradient in design view and using 'Convert to Resource':
<LinearGradientBrush x:Key="BackgroundGradient" EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#9BC6C5"/>
<GradientStop Color="#BAD8D7" Offset="1"/>
</LinearGradientBrush>
Any help would be greatly appreciated. The only way I can currently make it work is by removing the reference to the gradient in the Style definition, but I don't really want to have to set the Gradient on individual items.
Upvotes: 3
Views: 2181
Reputation: 2094
That's a very common issue, you must to ensure that this
<Style x:Key="MainBackground" TargetType="Panel">
<Setter Property="Background" Value="{StaticResource BackgroundGradient}"/>
</Style>
is executed after this
<LinearGradientBrush x:Key="BackgroundGradient" EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#9BC6C5"/>
<GradientStop Color="#BAD8D7" Offset="1"/>
</LinearGradientBrush>
If you have 2 or more files to setup this resources, Try fist to setup both styles in same file and ensure the Brush is defined before the Panel, then it works! so proceed to make it in individial files and watch about what file is executing first.
Upvotes: 3