If I had a Canvas with n number of Visual objects of any shape or size, how would I highlight/outline a Visual object programmatically?
Is there something built into WPF to help me?
If I had a Canvas with n number of Visual objects of any shape or size, how would I highlight/outline a Visual object programmatically?
Is there something built into WPF to help me?
If you wrap those Visual objects in a Border ... and make the Opacity 0 by default ... then you could programmatically turn the Opacity to 1 whenever you need to.
Does that help?
Having read the comments in the other answer -- in which you state that you wish the outline to conform to the shape of the visual -- I can only suggest you take a look at the BitmapEffects
. There is one for an Outer Glow effect.
Duplicating the visuals themselves would probably be less efficient, and would probably produce a great number of complications due to other aspects of those items, such as any bindings.
Edit: In .NET 4.0, BitmapEffects
property and the BitmapEffect
class are obsolete. You'll get an exception thrown. Instead, you now have the System.Windows.Media.Effects.Effect
class and its derived classes, along with properties such as Visual.VisualEffect
.
Joel has a great suggestion about using BitmapEffects.
However, if you can use .NET 3.5 SP1, I would steer you toward GPU rendered effects. So, instead of using UIElement.BitmapEffect, you would use UIElement.Effect. .NET 3.5 SP1 has two GPU effects built in: BlurEffect and DropShadowEffect. You can also create your own GPU effects ... and this is where you would use ShaderEffect.
Check out Greg Schechter's blog post series for more info.
Here is a piece of code for a mouse over effect that I just implemented a few days ago (it actually shows up sort of like an outer glow bitmap effect due to the ShadowDepth being 0, but not exactly):
<Path
x:Name="mouseOverEffect"
Width="80"
Height="43.916"
Stretch="None"
Fill="#FFFFFFFF"
Opacity="0"
>
<Path.Data>
<PathGeometry FillRule="Nonzero">
<PathFigure IsClosed="True" StartPoint="39.9592899612151,25.9913931634531">
<LineSegment Point="80.0000001464848,43.9159987905149"/>
<LineSegment Point="39.9513899394755,4.97379893856246E-14"/>
<LineSegment Point="1.77635636294422E-15,43.9159987905149"/>
<LineSegment Point="39.9592899612151,25.9913931634531"/>
</PathFigure>
</PathGeometry>
</Path.Data>
<Path.Effect>
<DropShadowEffect
Color="#FFFFFFFF"
BlurRadius="10"
ShadowDepth="0"
/>
</Path.Effect>
</Path>
I have some problem regarding this. I am running the code in in Window7. I have 2 Path and also the Path Effect and data=Geometry Object. But Effect is working for the Last Path only.For the First one Data is not displaying. If i will remove the Effect then it is displaying. This is the Problem with Window7 32 bit only. Please give me some idea.
<Border x:Name="brdSaved" Margin="5,2,0,12" CornerRadius="10" Grid.Row="0"
VerticalAlignment="Center" HorizontalAlignment="Left"
Background="Black">
<Path Name="pthSaved" Fill="White" SnapsToDevicePixels="True"
Margin="6,1,7,4" StrokeThickness="0.3" Stroke="White"
Effect="{DynamicResource tbEffect}"
VerticalAlignment="Center" HorizontalAlignment="Right"/>
</Border>
Effect="{DynamicResource tbEffect}" VerticalAlignment="Center" HorizontalAlignment="Right"/>
In CS am assigning Geometry Object to Data