issue to get RadialGradientBrush in wpf - wpf

<Border CornerRadius="20" BorderThickness="1" HorizontalAlignment="Left" Height="36" Margin="168,88,0,0" VerticalAlignment="Top" Width="36" BorderBrush="#FFBDD8D7">
<Border.Background>
<RadialGradientBrush GradientOrigin="0.317,-0.336" RadiusY="0.746" RadiusX="0.667">
<RadialGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterY="0.5" CenterX="0.5" ScaleY="1" ScaleX="1"/>
<SkewTransform AngleY="0" AngleX="0" CenterY="0.5" CenterX="0.5"/>
<RotateTransform Angle="174.743" CenterY="0.5" CenterX="0.5"/>
<TranslateTransform/>
</TransformGroup>
</RadialGradientBrush.RelativeTransform>
<GradientStop Color="#FF0099FF" Offset="0.051"/>
<GradientStop Color="#FF00090A" Offset="1"/>
<GradientStop Color="#FF33CCFF" Offset="0.761"/>
<GradientStop Color="#FF24BDFF" Offset="0.438"/>
</RadialGradientBrush>
</Border.Background>
</Border>
It's Result will be like below
But I need the Result Like Below.What should i do for that ?

Try something like this:
<Window x:Class="WpfApplication16.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<RadialGradientBrush x:Key="GlowFX" GradientOrigin="0.75,0.75" Center="0.5,0.5">
<GradientStop Color="#0073c0" Offset="1"/>
<GradientStop Color="#FF33CCFF" Offset="0.861"/>
<GradientStop Color="#FF24BDFF" Offset="0.438"/>
</RadialGradientBrush>
</Window.Resources>
<Grid>
<Border CornerRadius="20" BorderThickness="1" HorizontalAlignment="Left" Height="36" Margin="168,88,0,0" VerticalAlignment="Top" Width="36" BorderBrush="#FFBDD8D7"
Background="{StaticResource GlowFX}">
</Border>
</Grid>

Related

How do I add this ellipse template to my button border?

I'm aware there are probably similar questions to this one but I have no idea how to sort this out. I'm rather new to XAML and have written the following:
<Window.Resources>
<ControlTemplate x:Key="ButtonTemplate">
<Ellipse Height="300" StrokeThickness="2" Width="300" >
<Ellipse.Fill>
<SolidColorBrush Color="white" Opacity="0"/>
</Ellipse.Fill>
<Ellipse.Stroke>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="Red" Offset="0.1"/>
<GradientStop Color="Orange" Offset="0.35"/>
<GradientStop Color="SeaGreen" Offset="0.7"/>
<GradientStop Color="DarkBlue" Offset="0.95"/>
</LinearGradientBrush>
</Ellipse.Stroke>
</Ellipse>
</ControlTemplate>
</Window.Resources>
<Grid>
<Button Height="320" Width="320" Margin="200,100,200,100" Background="White" BorderThickness="0">
<Button.Foreground>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Crimson" Offset="0.1" />
<GradientStop Color="SeaGreen" Offset="0.9" />
</LinearGradientBrush>
</Button.Foreground>
<Button.Content>
<TextBlock FontSize="30">TEST BUTTON</TextBlock>
</Button.Content>
</Button>
</Grid>
Putting the circle in a template seems to be the universally agreed way to start going about getting a circular border on the button, but nothing seems to work from there for me. I've left the template disconnected from the button here because it doesn't work, rather it seems to just overlay a solid circle on top of what is otherwise a working button. Can someone help me out?
You should specify the TargetType and add a ContentPresenter to the ControlTemplate:
<ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
<Grid>
<Ellipse Height="300" StrokeThickness="2" Width="300" >
<Ellipse.Fill>
<SolidColorBrush Color="White" Opacity="0"/>
</Ellipse.Fill>
<Ellipse.Stroke>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="Red" Offset="0.1"/>
<GradientStop Color="Orange" Offset="0.35"/>
<GradientStop Color="SeaGreen" Offset="0.7"/>
<GradientStop Color="DarkBlue" Offset="0.95"/>
</LinearGradientBrush>
</Ellipse.Stroke>
</Ellipse>
<ContentPresenter Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
And don't forget to apply the custom template by setting the Template property of the Button:
<Button Height="320" Width="320" Background="White" BorderThickness="0"
Template="{StaticResource ButtonTemplate}">
<Button.Foreground>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Crimson" Offset="0.1" />
<GradientStop Color="SeaGreen" Offset="0.9" />
</LinearGradientBrush>
</Button.Foreground>
<Button.Content>
<TextBlock FontSize="30">TEST BUTTON</TextBlock>
</Button.Content>
</Button>

WPF enabling transparency

Going off the how-to from MSDN here, I have the following code that displays a reflection right below an element.
How can I enable transparency on the reflection only, so what is behind the window shows through the reflection?
<Window x:Class="XAMLViewTests.AboutWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AboutWindow" ResizeMode="CanResizeWithGrip" SizeToContent="WidthAndHeight" Width="400"
AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="CenterOwner" Background="Transparent">
<Window.Resources>
<Style TargetType="TextBlock" x:Key="formattedText">
<Setter Property="FontFamily" Value="Calibri"></Setter>
<Setter Property="FontSize" Value="20"></Setter>
<Setter Property="Padding" Value="5,5,5,5"></Setter>
<Setter Property="TextWrapping" Value="Wrap"></Setter>
</Style>
<LinearGradientBrush x:Key="linearGradBrush">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.0"
Color="White"></GradientStop>
<GradientStop Offset="1.0"
Color="LightSlateGray"></GradientStop>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Window.Resources>
<StackPanel Orientation="Vertical" Background="{StaticResource linearGradBrush}">
<Button Width="50" HorizontalAlignment="Right" Click="ReturnToPreviousWindow" Background="White">Return</Button>
<TextBlock Style="{StaticResource formattedText}" x:Name="textBlock" HorizontalAlignment="Center">
Some text here.
</TextBlock>
<!-- Reflection visual courtesy of MS How-To at https://msdn.microsoft.com/en-us/library/aa970263(v=vs.110).aspx -->
<Rectangle Height="1" Fill="Gray" HorizontalAlignment="Stretch"></Rectangle>
<Rectangle Height="{Binding ElementName=textBlock, Path=ActualHeight}"
Width="{Binding ElementName=textBlock, Path=ActualWidth}"
HorizontalAlignment="{Binding ElementName=textBlock, Path=HorizontalAlignment}">
<Rectangle.Fill>
<VisualBrush Stretch="None" Visual="{Binding ElementName=textBlock}">
<VisualBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="-1"></ScaleTransform>
<TranslateTransform Y="1"></TranslateTransform>
</TransformGroup>
</VisualBrush.RelativeTransform>
</VisualBrush>
</Rectangle.Fill>
<Rectangle.OpacityMask>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#FF000000" Offset="0.0"></GradientStop>
<GradientStop Color="#33000000" Offset="0.5"></GradientStop>
<GradientStop Color="#00000000" Offset="0.9"></GradientStop>
</LinearGradientBrush>
</Rectangle.OpacityMask>
<Rectangle.Effect>
<BlurEffect Radius="2.5"></BlurEffect>
</Rectangle.Effect>
</Rectangle>
</StackPanel>
</Window>
The linear gradient brush has 100% opacity and is applied to the entire stackpanel which contains the element you want transparency on. I reformatted it a bit and split out the stackpanel. Probably needs more work, but this should demonstrate the concept. Note the second gradient brush with a 0.5 (50%) opacity applied to the second stackpanel.
<Window x:Class="XAMLViewTests.AboutWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AboutWindow" ResizeMode="CanResizeWithGrip" SizeToContent="WidthAndHeight" Width="400"
AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="CenterOwner" Background="Transparent">
<Window.Resources>
<Style TargetType="TextBlock" x:Key="formattedText">
<Setter Property="FontFamily" Value="Calibri"></Setter>
<Setter Property="FontSize" Value="20"></Setter>
<Setter Property="Padding" Value="5,5,5,5"></Setter>
<Setter Property="TextWrapping" Value="Wrap"></Setter>
</Style>
<LinearGradientBrush x:Key="linearGradBrush">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.0"
Color="White"></GradientStop>
<GradientStop Offset="1.0"
Color="LightSlateGray"></GradientStop>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
<LinearGradientBrush x:Key="linearGradBrushWithTransparency" Opacity="0.5">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.0"
Color="White"></GradientStop>
<GradientStop Offset="1.0"
Color="LightSlateGray"></GradientStop>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Window.Resources>
<StackPanel Orientation="Vertical" Background="{StaticResource linearGradBrushWithTransparency}">
<StackPanel Orientation="Vertical" Background="{StaticResource linearGradBrush}">
<Button Width="50" HorizontalAlignment="Right" Click="ReturnToPreviousWindow" Background="White">Return</Button>
<TextBlock Style="{StaticResource formattedText}" x:Name="textBlock" HorizontalAlignment="Center">
Some text here.
</TextBlock>
<!-- Reflection visual courtesy of MS How-To at https://msdn.microsoft.com/en-us/library/aa970263(v=vs.110).aspx -->
<Rectangle Height="1" Fill="Gray" HorizontalAlignment="Stretch"></Rectangle>
</StackPanel>
<Rectangle Height="{Binding ElementName=textBlock, Path=ActualHeight}"
Width="{Binding ElementName=textBlock, Path=ActualWidth}"
HorizontalAlignment="{Binding ElementName=textBlock, Path=HorizontalAlignment}"
Opacity=".5">
<Rectangle.Fill>
<VisualBrush Stretch="None" Visual="{Binding ElementName=textBlock}">
<VisualBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="-1"></ScaleTransform>
<TranslateTransform Y="1"></TranslateTransform>
</TransformGroup>
</VisualBrush.RelativeTransform>
</VisualBrush>
</Rectangle.Fill>
<Rectangle.OpacityMask>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#FF000000" Offset="0.0"></GradientStop>
<GradientStop Color="#33000000" Offset="0.5"></GradientStop>
<GradientStop Color="#00000000" Offset="0.9"></GradientStop>
</LinearGradientBrush>
</Rectangle.OpacityMask>
<Rectangle.Effect>
<BlurEffect Radius="2.5"></BlurEffect>
</Rectangle.Effect>
</Rectangle>
</StackPanel>
</Window>

RadialGradientBrush on Rounded-Corner Rectangle

I have a Rectangle with rounded corners (but not an ellipse), something like this:
<Rectangle Stroke="Black" StrokeThickness="2" RadiusX="50" RadiusY="100">
<Rectangle.Fill>
<RadialGradientBrush RadiusY="0.5">
<GradientStop Color="Black" Offset="1" />
<GradientStop Color="White" Offset="0.8" />
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
I want to use a gradient fill from black to white. How can I specify this in order to make the fill keep the shape of the rounded rectangle, instead of being an ellipse?
For a rounded rectangle you can do it all in XAML using radial gradients for the corners and linear gradients for the sides.
The example uses a ViewBox so the Size doesn't need to be set both on the grid and its clip path. If you need it to resize keeping the same border radius you could bind RectangleGeometry.Rect and use a ValueConverter. The gradient and the RadiusX and RadiusY properties can be easily changed in one place.
<Viewbox Stretch="Fill">
<Grid Width="100" Height="100">
<Grid.Resources>
<Color x:Key="inside">White</Color>
<GradientStopCollection x:Key="gradient">
<GradientStop Color="Black" Offset="1"/>
<GradientStop Color="{DynamicResource inside}" Offset="0.2"/>
</GradientStopCollection>
</Grid.Resources>
<Grid.Clip>
<RectangleGeometry RadiusX="15" RadiusY="30" Rect="0,0,100,100" x:Name="clip" />
</Grid.Clip>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding RadiusX, ElementName=clip}" />
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="{Binding RadiusX, ElementName=clip}" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="{Binding RadiusY, ElementName=clip}"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="{Binding RadiusY, ElementName=clip}"/>
</Grid.RowDefinitions>
<Rectangle Grid.Column="1" Margin="-1,0">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0,0" MappingMode="RelativeToBoundingBox" StartPoint="0,1" GradientStops="{DynamicResource gradient}" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Column="2" Grid.Row="1" Margin="0,-1">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1,0" MappingMode="RelativeToBoundingBox" StartPoint="0,0" GradientStops="{DynamicResource gradient}" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Column="1" Grid.Row="2" Margin="-1,0">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0" GradientStops="{DynamicResource gradient}" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Row="1" Margin="0,-1">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0,0" MappingMode="RelativeToBoundingBox" StartPoint="1,0" GradientStops="{DynamicResource gradient}" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Column="1" Grid.Row="1" Margin="-1">
<Rectangle.Fill>
<SolidColorBrush Color="{DynamicResource inside}" />
</Rectangle.Fill>
</Rectangle>
<Rectangle>
<Rectangle.Fill>
<RadialGradientBrush Center="1,1" RadiusX="1" RadiusY="1" GradientOrigin="1,1" GradientStops="{DynamicResource gradient}" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Column="2">
<Rectangle.Fill>
<RadialGradientBrush Center="0,1" RadiusX="1" RadiusY="1" GradientOrigin="0,1" GradientStops="{DynamicResource gradient}" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Column="2" Grid.Row="2">
<Rectangle.Fill>
<RadialGradientBrush Center="0,0" RadiusX="1" RadiusY="1" GradientOrigin="0,0" GradientStops="{DynamicResource gradient}" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Row="2">
<Rectangle.Fill>
<RadialGradientBrush Center="1,0" RadiusX="1" RadiusY="1" GradientOrigin="1,0" GradientStops="{DynamicResource gradient}" />
</Rectangle.Fill>
</Rectangle>
</Grid>
</Viewbox>
If you need to a gradient to follow more complex shapes you can do it with a V3.0 PixelShader.
Here is a simple example composing a rounded-rectangle gradient out of more primitive gradients:
<Canvas>
<Canvas.Resources>
<GradientStopCollection x:Key="stops">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="Black" Offset="1"/>
</GradientStopCollection>
<RadialGradientBrush x:Key="cornerBrush" GradientStops="{StaticResource stops}"/>
<LinearGradientBrush x:Key="topBrush" StartPoint="0,1" EndPoint="0,0" GradientStops="{StaticResource stops}"/>
<LinearGradientBrush x:Key="leftBrush" StartPoint="1,0" EndPoint="0,0" GradientStops="{StaticResource stops}"/>
<LinearGradientBrush x:Key="rightBrush" StartPoint="0,0" EndPoint="1,0" GradientStops="{StaticResource stops}"/>
<LinearGradientBrush x:Key="bottomBrush" StartPoint="0,0" EndPoint="0,1" GradientStops="{StaticResource stops}"/>
</Canvas.Resources>
<Ellipse Canvas.Left="0" Canvas.Top="0" Width="100" Height="100" Fill="{StaticResource cornerBrush}"/>
<Ellipse Canvas.Left="200" Canvas.Top="0" Width="100" Height="100" Fill="{StaticResource cornerBrush}"/>
<Ellipse Canvas.Left="0" Canvas.Top="200" Width="100" Height="100" Fill="{StaticResource cornerBrush}"/>
<Ellipse Canvas.Left="200" Canvas.Top="200" Width="100" Height="100" Fill="{StaticResource cornerBrush}"/>
<Rectangle Canvas.Left="50" Canvas.Top="0" Width="200" Height="50" Fill="{StaticResource topBrush}"/>
<Rectangle Canvas.Left="0" Canvas.Top="50" Width="50" Height="200" Fill="{StaticResource leftBrush}"/>
<Rectangle Canvas.Left="250" Canvas.Top="50" Width="50" Height="200" Fill="{StaticResource rightBrush}"/>
<Rectangle Canvas.Left="50" Canvas.Top="250" Width="200" Height="50" Fill="{StaticResource bottomBrush}"/>
<Rectangle Canvas.Left="50" Canvas.Top="50" Width="200" Height="200" Fill="White"/>
</Canvas>
which produces this effect:

WPF listbox crash on XP

Why does this listbox crash on windows XP and not Vista or windows 7,
The it builds fine without error but when iam trying to run it in xp in crashed and i get this error..
System.FormatExeption
<ListBox Name="lvMyAssignments" ScrollViewer.VerticalScrollBarVisibility="Hidden" Height="280" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Margin="0,3,0,0" Tag="{Binding Path=Plocklista}" Background="Transparent" Click="Open_Assignment">
<StackPanel>
<Border BorderBrush="Black" BorderThickness="1,1,1,0">
<Border.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Value="True">
<Setter Property="Border.Height" Value="100"/>
<Setter Property="Border.Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" MappingMode="RelativeToBoundingBox">
<GradientStop Color="DarkGray" Offset="1"/>
<GradientStop Color="#FFE8E8E8"/>
<GradientStop Color="#FFBDBDBD" Offset="0.153"/>
<GradientStop Color="DarkGray" Offset="0.904"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" MappingMode="RelativeToBoundingBox">
<GradientStop Color="#FFECECEC" Offset="1"/>
<GradientStop Color="#FFE8E8E8"/>
<GradientStop Color="#FFBDBDBD" Offset="0.153"/>
<GradientStop Color="#FFE8E8E8" Offset="0.904"/>
</LinearGradientBrush>
</Border.Background>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Border BorderBrush="Darkgray" BorderThickness="0,0,1,0">
<DockPanel Width="555">
<ItemsControl Height="70" ItemsSource="{Binding Path=Descriptions}" Background="Transparent" BorderThickness="0" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock FontWeight="Bold" Text="{Binding Path=Value}"/>
<TextBlock FontWeight="Bold" Margin="5,0,0,0" Text="{Binding Path=Key}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DockPanel>
</Border>
<StackPanel HorizontalAlignment="Center">
<StackPanel.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<LinearGradientBrush.RelativeTransform>
<TransformGroup>
<TranslateTransform X="-0.5" Y="-0.5"/>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="90"/>
<TranslateTransform X="0.5" Y="0.5"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="sc#1, 1, 0.158960834, 0.004391442" Offset="0"/>
<GradientStop Color="sc#1, 1, 0.5, 0.5" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background>
<StackPanel HorizontalAlignment="Center">
<StackPanel.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<LinearGradientBrush.RelativeTransform>
<TransformGroup>
<TranslateTransform X="-0.5" Y="-0.5"/>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="90"/>
<TranslateTransform X="0.5" Y="0.5"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="sc#1, 1, 0.158960834, 0.004391442" Offset="0"/>
<GradientStop Color="sc#1, 1, 0.5, 0.5" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background>
<TextBlock Text="{Binding Path=Antal}" TextAlignment="Center" Width="100" FontSize="44pt" FontFamily="Calibri"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Border>
</StackPanel>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
The only thing I see in your XAML that might be affected by OS version is Font="Calibri", since XP will have different fonts installed than Vista / Win7, and WPF has its own font handling. Maybe that is it, but probably not.
I suggest you post a stack trace of the FormatException you get. This might give some real clues. Also try to boil down your XAML to the mimimum necessary to give the error. Then we will be more likely to see it.

How to draw a drop-shadow on a transparent rectangle?

When I add a dropshadow bitmap effect to a rectangle, the dropshadow takes into account the transparency of the rectangle (makes sense). Is there a way to render a dropshadow on a transparent rectangle 'as if' the rectangle were opaque? ie what would appear is a rectangle-shaped 'hole', with a dropshadow.
Here is the XAML for a transparent rectangle with a dropshadow - nothing is displayed:
<Rectangle Fill="Transparent" Margin="10" Width="100" Height="100">
<Rectangle.BitmapEffect>
<DropShadowBitmapEffect/>
</Rectangle.BitmapEffect>
</Rectangle>
Drop this into Kaxaml. It creates a transparent Rectangle of size 500x500, with a SystemDropShadowChrome Decorator. The drop shadow's clip is set to exclude the Rectangle's region.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas>
<theme:SystemDropShadowChrome Margin="0,0,5,5">
<Rectangle Width="500" Height="500" Fill="transparent"/>
<theme:SystemDropShadowChrome.Clip>
<CombinedGeometry GeometryCombineMode="Exclude">
<CombinedGeometry.Geometry1>
<RectangleGeometry Rect="0,0,505,505"/>
</CombinedGeometry.Geometry1>
<CombinedGeometry.Geometry2>
<RectangleGeometry Rect="0,0,500,500"/>
</CombinedGeometry.Geometry2>
</CombinedGeometry>
</theme:SystemDropShadowChrome.Clip>
</theme:SystemDropShadowChrome>
</Canvas>
</Page>
If you want your drop shadow to have rounded corners, then set the CornerRadius of the SystemDropShadowChrome to whatever (let's say 10), then Geometry1's Left and Top values to 10, then the RadiusX and RadiusY of each RectangleGeometry to 10.
I'd love to see better solution, but here is what I usually do (beware: creepy code ahead).
Wrap rectangle to three-four rectangles, and play with stroke color, making it darker and darker as it goes to original rectangle. Here is the code:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Rectangle
Width="106"
Height="106"
Stroke="#10000000"
StrokeThickness="1"/>
<Rectangle
Width="104"
Height="104"
Stroke="#5C000000"
StrokeThickness="1"/>
<Rectangle
Width="102"
Height="102"
Stroke="#AC000000"
StrokeThickness="1"/>
<Rectangle
Width="100"
Height="100"
Fill="Transparent"
Stroke="#FF000000"
StrokeThickness="1">
</Rectangle>
</Grid>
</Page>
This gives you:
alt text http://img521.imageshack.us/img521/7664/shadowo.jpg
Another approach would be with borders - it's better because you don't have to specify dimensions, when you put them inside Grid.
And the best approach (never seen implemented though): custom pixel shader, which makes what you want.
Well, here is one long-winded way to implement a rectangular 'drop-shadow' without using a bitmap effect. In this case the centre of the 'shadow rectangle' is coloured in, but it could be set to transparent to give you a 'halo' style drop shadow (i.e., equal all the way round - not offset)
<UserControl x:Class="RectShadow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<UserControl.Resources>
<System:Double x:Key="CornerSize">5</System:Double>
<Color x:Key="ShadowColor">#60000000</Color>
<Color x:Key="TransparentColor">#00000000</Color>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Rectangle Width="{StaticResource CornerSize}" Height="{StaticResource CornerSize}">
<Rectangle.Fill>
<RadialGradientBrush Center="1,1" GradientOrigin="1,1" RadiusX="1" RadiusY="1">
<GradientStop Color="{StaticResource ShadowColor}"/>
<GradientStop Offset="1" Color="{StaticResource TransparentColor}"/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Row="2" Grid.Column="2" Width="{StaticResource CornerSize}" Height="{StaticResource CornerSize}">
<Rectangle.Fill>
<RadialGradientBrush Center="0,0" GradientOrigin="0,0" RadiusX="1" RadiusY="1">
<GradientStop Color="{StaticResource ShadowColor}"/>
<GradientStop Offset="1" Color="{StaticResource TransparentColor}"/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Row="0" Grid.Column="2" Width="{StaticResource CornerSize}" Height="{StaticResource CornerSize}">
<Rectangle.Fill>
<RadialGradientBrush Center="0,1" GradientOrigin="0,1" RadiusX="1" RadiusY="1">
<GradientStop Color="{StaticResource ShadowColor}"/>
<GradientStop Offset="1" Color="{StaticResource TransparentColor}"/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Row="2" Grid.Column="0" Width="{StaticResource CornerSize}" Height="{StaticResource CornerSize}">
<Rectangle.Fill>
<RadialGradientBrush Center="1,0" GradientOrigin="1,0" RadiusX="1" RadiusY="1">
<GradientStop Color="{StaticResource ShadowColor}"/>
<GradientStop Offset="1" Color="{StaticResource TransparentColor}"/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Column="1">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0,1">
<GradientStop Offset="1" Color="{StaticResource ShadowColor}"/>
<GradientStop Color="{StaticResource TransparentColor}"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Column="1" Grid.Row="2">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0,1">
<GradientStop Color="{StaticResource ShadowColor}"/>
<GradientStop Offset="1" Color="{StaticResource TransparentColor}"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Row="1">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1,0">
<GradientStop Offset="1" Color="{StaticResource ShadowColor}"/>
<GradientStop Color="{StaticResource TransparentColor}"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Row="1" Grid.Column="2">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1,0">
<GradientStop Color="{StaticResource ShadowColor}"/>
<GradientStop Offset="1" Color="{StaticResource TransparentColor}"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Grid.Row="1" Grid.Column="1">
<Rectangle.Fill>
<SolidColorBrush Color="{StaticResource ShadowColor}"/>
</Rectangle.Fill>
</Rectangle>
</Grid>
</UserControl>
wrap the rectangle in a border. and add a shadow to the border. you'll get the same effect.

Resources