How to add drop shadow to just one specific side in WPF? - 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.

Related

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.

binding the background color of a gradient inside of a WPF listbox datatemplate

So I have a datatemplate and there is board in it, here is what I wan to do.
<Border Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="5">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Transparent"/>
<GradientStop
Color="{Binding Condition, Converter={StaticResourc ConditionTypeToColorConveter}}" Offset="0.541"/>
</LinearGradientBrush>
</Border.Background>
</Border>
I can see the converter being called no problem. but the color it returns isn't displayed. Now if I do this
<Border Grid.Column="0"
Grid.Row="4" Grid.ColumnSpan="5"
Background="{Binding Condition, Converter={StaticResourc ConditionTypeToColorConveter}}">
It works just fine - of course - I don't want it to be all one color I need it to be a gradient.
Anyone have any idea what is wrong with this? it's making me batty...
This can't work because in the first example you bind a Color in the second a Brush. What type your converter returns? You named it 'ToColor' but it is working as a Brush for Background.

Wpf - Drop shadow disappears

I created a window with no style and added a custom border. I need my window to drop a shadow.
<Border BorderBrush="#000000 "
BorderThickness="1,0,1,1" Width="400"
Height="400"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
CornerRadius="10, 10, 0, 0"
Background="Black">
<Border.Effect>
<DropShadowEffect Color="Black" Direction="320"
ShadowDepth="5" Opacity="1" BlurRadius="5" />
</Border.Effect></Border>
But my shadow disappears when I set the width and height like this :
Width="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=Width}"
Help me find a solution please. Thanks
Since we don't have the full scope of your solution, I must assume that the Border is within a ControlTemplate that is assigned to the Style of the Window. Remember that if you are going to apply a DropShadowEffect to the root window you need to pad it otherwise you won't see it. Apply Padding="10" to Border and you should see it.
Try To add on the X & Y Thickness of the Shadow to your Windows dimensions
Don't apply effects on the root visuals! In this case, they apply to all children down visual tree and will be cause of reducing performance.
Use the next pattern:
<Grid x:Name="RootWindowGrid">
<Border x:Name="BorderWithEffect" Margin="5">
<Border.Effect>
<DropShadowEffect Color="Black" Direction="320" ShadowDepth="5" Opacity="1" BlurRadius="5" />
</Border.Effect>
</Border>
<Border x:Name="RootBorder_ForAll_Another_Window_Visuals" Margin="5"/>
</Grid>
Margin depends on effect intensivity.

Font Stretching with different monitor screen resolution

<Grid Name="maingrid">
<Viewbox Stretch="Fill" Height="Auto">
<dp:DockablePanel x:Name="dockPanel" Width="980" Height="710" Margin="0,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Center">
<dp:DockablePanel.DockPanelCollection>
<dp:ChildPanel StripTitle="" Dock="Left" ParentName="Window1" Eve_MenuItemClicked="menuAssets_GotMouseCapture" ScrollViewer.VerticalScrollBarVisibility="Hidden" >
<dp:ChildPanel.OpacityMask>
<LinearGradientBrush EndPoint="0.131,0.161" StartPoint="0.143,0.163">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</dp:ChildPanel.OpacityMask>
<dp:ChildPanel.Effect>
<DropShadowEffect BlurRadius="6" Color="#FF000000" Direction="-60" Opacity="0.68" ShadowDepth="0"/>
</dp:ChildPanel.Effect>
</dp:ChildPanel>
</dp:DockablePanel.DockPanelCollection>
<dp:DockablePanel.CenterContent>
<TextBlock></TextBlock>
</dp:DockablePanel.CenterContent>
</dp:DockablePanel>
</Viewbox>
where works ok on my system where my screen resolution is 1024 and 768,but the font (menu panel font ) gets stretched on running this exe on another system (with monitor screen resolution (1280 ,768) or (1366,768) ,please provide me a solution regarding this
When you expect your application to properly run in full-screen mode, you need to think about a real layout.
If your application has to run on systems with different aspect ratios like 4:3 (1024x768), 5:3 (1280x768) and 16:9 (1366x768), you can not simply put everything in a ViewBox and expect it to fill the entire screen and at the same time keep the aspect ratio.
Either you incorporate a layout that allows for different aspect ratios, or you simply don't fill the screen. Consider dropping your top-level ViewBox and read about the layout options offered by e.g. Grid.
<Viewbox Stretch="Uniform" Height="Auto">

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