Custom Fill Property on PathGeometry in Silverlight - wpf

I've been looking at (and getting confused by) Dependency Properties - I'm not sure if this is what I need or if there is something else.
I'm looking to something very specific with <Path.Data/> children in Silverlight, in particular <PathGeometry/>, <EllipseGeometry/>, etc. While the <Path/> element has a .Fill property, I'd like to add a .Fill property to any of it's Geometries, meaning it's a different color from it's parent. It could be a <SolidColorBrush/> or <LinearGradientBrush/> color, or a percentage of the parent color (like 20% darker than <Path.Fill/>.
Is this possible? Is this a dependency property? How would <RectangleGeometry/>, for example, know that I am trying to fill it with a color? How would I get started?
(adding WPF as a tag too as someone who knows WPF may be able to help)

Not its not possible, the stroke and fill brush are properties of the Path shape.
If you want to draw Rectangle, Ellipse etc shapes with their own independent Stroke and Fill properties you need to use Rectangle and Ellipse which are Shape objects in their own right.

Related

WPF InkCanvas Z-Index for strokes

In WPF InkCanvas, ink is rendered on top and strokes don't have z-index property. I have other UIElements like shape, images on InkCanvas and can't bring UIElements to top. Is there any work around for this problem. I have tried
below things.
1) use two different InkCanvas (one for strokes & one for UIElements)
- Can't achieve sandwich effect in this (i.e. UIElement between strokes
Something like this in this circle is UIElement
2) Masking strokes using geometry path and add as UIElement
- If I add strokes as UIElements (using path) then I will loose erase functionality.
The map editor which is part of our game suite uses ink to draw terrain so I have recent experience of this sort of thing.
The way my app works is you select a thing you're drawing ( woods, road, contour etc ) and draw a stroke using ink.
I then process that stroke and obtain a geometry.
I pass that to a viewmodel > itemssource > itemscontrol.
This is then templated into a piece of terrain.
This itemscontrol is below the inkcanvas.
The user can manipulate the z-index of those by moving them up and down in the observablecollection.
I rely on just the order to define z-index.
I also retain the data from each stroke by serialising it.
You're thinking "how about erase?".
To do that I'd have an explicit erase-this-thing mode.
Let them pick a shape and get it's stroke data, add the stroke back to the inkcanvas. Once they finish erasing then use a "save changes" button or something to re-process the stroke(s) and switch back to edit mode.
Since this is a game map editor I'm working on, drawing produces area and line types of terrain but their area/line is defined by a geometry translated from a stroke. ZIndex corresponds to the order of objects in the left panel. The buttons you can see with the chevrons pointing up and down move the order of a

TreeView fontsize - Custom Draw or Owner Draw

I want to change the font size of a TreeView control. After searching on goolge, I just get some idea that one should use CustomDraw for this TreeView. But NMTVCUSTOMDRAW structure only has clrText and clrText which set the foreground and background color of the displayed texts in TreeView.
My questions are:
Q1: How to change the font size in a TreeView? For the background color of TreeView,
one can just use `TreeView_SetBkColor` or send a message. Anything similar
for font size?
Q2: What's the difference between Custom Draw and Owner Draw?
Q1. You can do this with the WM_SETFONT message.
Q2. They do similar things. Owner draw is an older system and means you have to draw the entire control yourself. Custom draw is newer and more flexible, letting you handle just parts of the drawing if you like (or even no drawing, and just changing fonts/colors).

What is the best way to copy / clone brushes?

Before drawing a shape on a canvas I have a preview that displays how the shape will look. I can adjust the opacity and then draw the shape. I may then wish to draw a second shape with a different opacity. My problem is that altering the opacity of the preview also alters the opacity of the shape that I have already drawn.
This has led me to believe that I need to create a copy of the brush used for the preview each time before drawing the shape.
There are various different brushes and for example, the gradient brushes require making a copy of the not just the gradient stop collection, but a new gradient stop for each gradient stop in the to-be-copied collection.
Am I down the right track here or should I be doing something else? Should I be copying or cloning? Would an extension method be the best way to go? Thoughts please.
What you need is cloning, it would be easy in wpf with XamlWriter/Reader, unfortunately you cannot do it in Silverlight. An extension method on Brush that makes a deep copy would work fine in your case though. You will have to handle the different brush type separately but it should not be an issue as there aren't that many.

Panel with percentage coordinates

I would like use a panel whose children have coordinates specified as percentage of total panel's width/height. Moreover, I should be able to animate the coordinate property, for example to make a button move from 10% to 50% panel's width.
I've made 2 attempts:
Use a Grid and specify size as stars - this was not enough, because AFAIK by default WPF cannot animate distance properties specified by stars. I've found somewhere a custom class that enabled me to do so, it even worked, hovewer I consider that solution overly complicated an I am looking for something simpler.
Use a Canvas with fixed width and height and put it inside a Viewbox - this is a simple solution, but when resizing the Viewbox the whole content of Canvas is resized too. I want the content to have fixed size.
Is there a simple solution or should I implement my own panel (or maybe extend one of the existing ones, i.e. Canvas)?
Cheers!
I would:
subclass Canvas, perhaps calling it RelativeCanvas or RatioCanvas
add two attached properties: XRatio and YRatio
override ArrangeOverride and loop over all children. For each child, use their XRatio and YRatio along with the ActualWidth and ActualHeight of the RelativeCanvas to calculate and apply values for their Canvas.Left and Canvas.Top attached properties
You would use it as follows:
<local:RelativeCanvas>
<!-- the top-left of this button will be center of panel -->
<Button local:RelativeCanvas.XRatio="50" local:RelativeCanvas.YRatio="50"/>
</local:RelativeCanvas>
One thing you might like to add after you get that working is control over alignment. For example, I might to align the center of a control to the specified ratio, not its top-left corner.
There's one here: WPF Proportional Panel

Morphing from an image to a shape in Silverlight 3

I have a requirement to morph from an image (png) to a shape (polygon) in Silverlight 3 as an effect, but of course there is no built in transition or method to do this.
At the moment the best I have is fade one out and the other in, but can anyone suggest a decent alternative that may work or look better?
Regards
Moo
In Blend:
Create a rectangle. Set stroke to No Brush and Fill to Tile Brush.
For the ImageBrush of the Tile Brush, select your image.
In the object browser, select the rectangle, right click > Path > Convert to Path.
Use the pen tool to add some points to the path.
Add a storyboard.
Add a keyframe at 1 second. Blend will go into record mode
Use the direct selection tool to move the points into the polygon shape you want. Test out your animation.
At this point, the image morphs into a shape, but the image is still there. If you need to remove the image as well as morph it:
In your storyboard, at the keyframe at 1 second, change the opacity to 0.
Create a copy of the rectangle, but make sure fill is set to No Brush and Stroke is set to a color and width. Set the opacity to 0.
Add points, and mimic the animation you just set up for the image's rectangle.
Add a keyframe at 1 second for this element. In record mode, change the opacity to 100%.
The end result will be both paths morphing, the one with the image fading out while the one with no fill fading in.
I'm not a silverlight programmer, and don't know the details of what you want to do, so this is just a shot in the dark, but... if the shape you want the image to morph to is always going to have the same initial visual appearance (or some limited set of appearances) you might try morphing from the original image to an image of that shape, and then swapping the image that's the morph target for the geometry once you're done the morph. Course whether that would work or not is very dependent on the details of what you're doing. Sorry if you've already considered that and ruled it out.
You could possibly morph the image brush to the path of the shape by using an appropriate projection matrix. Or render a shape using the image brush and then morph that shape to the target shape, i.e. go from a rectangle to the target shape but using the image brush as the shape background. You may need to still warp the image brush somehow though.
An example of rendering a warped image is here in Charles Petzold's blog.

Resources