wpf gradient brush that spans multiple elements - wpf

Suppose there is an element with irregular shape composed of other elements with arbitrary nesting:
<Window.Resources>
<RadialGradientBrush x:Key="brush">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="White" Offset="1" />
</RadialGradientBrush>
</Window.Resources>
<StackPanel Name="element">
<StackPanel.Effect>
<DropShadowEffect ShadowDepth="0" BlurRadius="10" />
</StackPanel.Effect>
<Rectangle Name="child1" Height="100" Margin="10" Stroke="Black" Fill="{StaticResource brush}" />
<Grid>
<Rectangle Name="child2" Height="100" Margin="10" Stroke="Black" Fill="{StaticResource brush}" />
</Grid>
</StackPanel>
All parts are interactive (i.e a child can be a real control).
How can I fill backgrounds of children with a single radial gradient that spans all of them (should look like as if it is sized to element).
UPDATE: StackPanel has a shadow which should be drawn around children.
One possible solution is to generate gradients with Radius and Origin/Center bound to element's and child's properties with appropriate conversions, but such approach would be rather complex and expensive.

Finally, I went with transformed gradients route except instead of bindings and converters I was able to elegantly solve it by creating background Transformation in ArrangeOverride of child elements and exposing it via dependency property.
Actual RadialGradientBrush binds its Transform to BackgroundTransform by searching for ancestor with matching type.
This assumes that one can modify/wrap child classes which is true in my case. It can be made more flexible with attached properties and a bit more work.
Actual code is in this gist.

Related

How to add drop shadow to just one specific side in WPF?

I am trying to achieve a grid, with a shadow on just one side and no trace of any shadow on any of the other sides. I tried fiddling around with the direction property of the DropShadowEffect.
What I have tried:
<Grid Background="Transparent" Grid.Row="0" Grid.Column="1">
<Grid Background="White"/>
<Border CornerRadius="0,5,0,0" BorderBrush="White" BorderThickness="0" Background="White">
<Border.Effect>
<DropShadowEffect BlurRadius="5" Direction="355" RenderingBias="Quality" ShadowDepth="2"/>
</Border.Effect>
</Border>
</Grid>
</Grid>
This is what happens with my code:
I want to achieve a drop shadow only visible on the bottom side of the grid, and no trace of the shadow on any of the other sides. The above code leaves a thin gray trail on the left side, which wouldn't work for me.
Sorry if this is a silly question, I am kinda new to WPF.
I don't think the DropShadowEffect has any functionality built-in for this sort of application, however, I managed to achieve the required result using a rectangle and filling it with a linear gradient.
<Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="0.3">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#00131313" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
To maintain the same width as the parent of the shadow, add them to the same grid and the same column, set the horizontal and vertical alignment to stretch and the shadow will look consistent.
Then I positioned the rectangle in place of the shadow. Seems a little wanky, but works nonetheless.
Edit:
I found another solution which seems way more better, using the ClipToBounds property and the BorderThickness property.
<Border ClipToBounds="True" BorderBrush="White" BorderThickness="0,2,0,0">
<Border.Effect>
<DropShadowEffect ShadowDepth="2" BlurRadius="10"/>
</Border.Effect>
</Border>
Using a border and a drop shadow is easier than using a rectangle and tweaking it till it looks natural.
Usage of grids is advised to position the border perfectly.

Create semi-circle blur effect on a Grid

I want to develop a WindowsPhone 8 app with a home page like the fourth phone on this page. The one with a mountain.
You can see an image with two layers over it. My problem is that I don't know how to call that effect in English, and also, I don't know how to do it.
Maybe I will need to make three copies of my image: one untouched, a second one with, a blur effect? and a third one with a 'bigger' blur effect.
Or maybe, I have to add two Grids with some white background and with an opacity less than 100%.
How would you do it?
Your solution is just a good one, I would the samething, and I don't expect that application to be implementing something different, in détails I would do :
One Grid with High Blur on the background.
Another Grid with middle Blur on the front. I'll use an OpacityMask set to a RadialGradientBrush to create that ring effect just like in here : http://msdn.microsoft.com/en-us/library/bb979637(v=vs.95).aspx
And one lase Grid with no Blur in the Front of The previous Grid again with a OpacityMask also set to a RadialGradientBrush.
The three Grids will all have the same Background TileBrush, example :
The Result I've made :
Code :
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="The effect you asked, with :" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="That's Clapton" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel1" Grid.Row="1" Margin="12,0,12,0">
<Grid.Background>
<ImageBrush ImageSource="Eric_Clapton_Blur3.jpg" Stretch="UniformToFill"/>
</Grid.Background>
</Grid>
<Grid x:Name="ContentPanel2" Grid.Row="1" Margin="12,0,12,0">
<Grid.OpacityMask>
<RadialGradientBrush Center="0.5,0.5">
<RadialGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" TranslateX="0.4" ScaleX="1.4"/>
</RadialGradientBrush.RelativeTransform>
<!-- This gradient stop is partially transparent. -->
<GradientStop Color="#00000000" Offset="1" />
<!-- This gradient stop is partially transparent. -->
<GradientStop Color="#20000000" Offset="0.741" />
<!-- This gradient stop is fully opaque. -->
<GradientStop Color="#FF000000" Offset="0.728" />
</RadialGradientBrush>
</Grid.OpacityMask>
<Grid.Background>
<ImageBrush ImageSource="Eric_Clapton_Blur2.jpg" Stretch="UniformToFill"/>
</Grid.Background>
</Grid>
<Grid x:Name="ContentPanel3" Grid.Row="1" Margin="12,0,12,0">
<Grid.OpacityMask>
<RadialGradientBrush Center="0.5,0.5">
<RadialGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" TranslateX="0.4" ScaleY="0.6"/>
</RadialGradientBrush.RelativeTransform>
<!-- This gradient stop is partially transparent. -->
<GradientStop Color="#00000000" Offset="1" />
<!-- This gradient stop is partially transparent. -->
<GradientStop Color="#20000000" Offset="0.741" />
<!-- This gradient stop is fully opaque. -->
<GradientStop Color="#FF000000" Offset="0.728" />
</RadialGradientBrush>
</Grid.OpacityMask>
<Grid.Background>
<ImageBrush ImageSource="Eric_Clapton.jpg" Stretch="UniformToFill"/>
</Grid.Background>
</Grid>
I know I didn't do it with perfection, and I used terrible naming for the Elements, It's just I've just woke up, and didn't even take my breakfast, hope that helps.

Why is the fill of an Ellipse not applied when the Stretch property is set inside a VisualBrush?

I'm working on a custom control and I have a VisualBrush with the Visual property as such:
<VisualBrush.Visual>
<Grid>
<Ellipse Stretch="Uniform" Stroke="OrangeRed" StrokeThickness="1">
<Ellipse.Fill>
<RadialGradientBrush GradientOrigin="0.5,0.5">
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Orange" Offset="1" />
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Ellipse Stretch="Uniform">
<Ellipse.Fill>
<RadialGradientBrush GradientOrigin="0.5, 0.05" RadiusX=".7" RadiusY=".5" >
<GradientStop Color="White" Offset=".10" />
<GradientStop Color="Transparent" Offset="1" />
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
</Grid>
</VisualBrush.Visual>
When the Stretch property is set to Uniform instead of using the RadialGradientBrush I created, it somehow creates a SolidColorBrush using the Stroke color. However, when I explicitly set the Width and Height properties of the Ellipse instead of using the Stretch property, I get the gradient I expected.
Any ideas of what's going weird here?
EDIT: I just observed the behavior occurs when I enclose the Ellipse inside of a ViewBox.
I'm not sure what you're using the ViewBox for in your above example, but I think some of the strange behaviour you're seeing is a result of not setting the size on the root element for your VisualBrush.
In the documentation for VisualBrush.Visual, there's a paragraph that mentions sizing (added emphasis):
When you define a new Visual for a VisualBrush and that Visual is a
UIElement (such as a panel or control), the layout system runs on the
UIElement and its child elements when the AutoLayoutContent property
is set to true. However, the root UIElement is essentially isolated
from the rest of the system; styles, storyboards, and external layout
dictated by the parent where the brush is applied cannot permeate this
boundary. Therefore, you should explicitly specify the size of the
root UIElement, because its only parent is the VisualBrush and
therefore it cannot automatically size itself to the area being
painted. For more information about layout in Windows Presentation
Foundation (WPF), see the Layout.
If I run your code above, my ellipse is completely filled with the stroke colour, presumably because the the layout is calculated with the smallest value for the ellipse, so the stroke is large enough to cover ellipse content/fill (which is then upscaled to the fill the viewport so it looks like the stroke is the fill colour).
If I give the root element some arbitrary size on which to base the layout <Grid Width="100" Height="100">...</grid> I start to see the stroke and fill colours, rendered with relative sizes.

Dynamic Brushes in XAML

I have a definition for a data template that looks as follows:
<DataTemplate DataType="{x:Type HeatMap:BlockItem}">
<Grid Visibility="{Binding IsVisible}">
<Border Name="BlockBorder" Width="{Binding Width}" Height="{Binding Height}">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{Binding Colour}" Offset="1"/>
<GradientStop Color="White"/>
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
</DataTemplate>
As can be seen, BlockItem has a property of type Color called Colour which is bound to the first color of the LinearGradientBrush which fills a border, making it look like a filled rectangle.
Now I don't always want Linear Gradient Brushes to style the fill of this rectangle. Some rectangles on my canvas may need to be filled with SolidBrushes, for example. I considered creating a Brush property on the BlockItem class instead of a Color property and binding the entire Border.Background to that, but there are 2 problems with this:
I don't know how the XAML should look to specify a binding to the entire object Background property.
In the code where I create BlockItems, if I instantiate a new Brush for every single BlockItem (bear in mind, there maybe be many drawn on a canvas at a time), will this not make it really inefficient and slow?
1) You can bind the Background directly to a brush on your BlockItem:
<Border Name="BlockBorder" Background="{Binding MyBackgroundBrush}">
2) You could bind to a static resource, or create a static brush for your BlockItem.
Background="{StaticResource myStaticBrush}"

How to set the back ground color of grid-column in wpf?

I have a wpf mvvm application.
And have a GRID with multiple columns
whats best way to set the back ground color of grid-column in wpf?
dabble125's answer was perfect but to give you a sample and to mention a note that it is important where to place your rectangle see the code:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- this is not a good place for text block.
the text block is beneath the rectangle
so it would not be seen -->
<!--<TextBlock Grid.Column="1" Text="Some Text"/>-->
<Rectangle Grid.Column="1" Grid.RowSpan="1000">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF83FF97" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock Grid.Column="1" Text="Some Text"/>
</Grid>
One way:
Create a rectangle and set its fill to the color of your choice.
Then set its Grid.RowSpan value to a large number or the number of rows you have.
Create a rectangle and set its fill to the color of your choice.
Only having :
<Rectangle
Grid.Column="1"
Fill="#e8ebf1" />
works for me.
The Grid.RowSpan of previous answers is actually useless, and the LinearGradientBrush demonstrated is over-complicated for what is asked.

Resources