My problem is I have paths to draw cities and I want to put images inside these cities I cant put Image inside <Path> tags.
Why cant I write such code? any solution?
You can use the Path.Fill for this. Use an ImageBrush.
On a side-note, what can be written as child node/s of a element is determined by the ContentPropertyAttribute, for a ContentControl it points to the Content property for example (as can be seen in the Syntax section of its documentation).
Use a canvas or a custom panel with x,y placements and add images to the correct coordinates (same coordinates used by path).
You can query the path nodes to find their coordinates if you don't know them.
Edit: Since your window size changes and coordinates "stretch" accordingly, you will need to implement a custom panel. - Inherit from Panel and override measure and arrange methods as in this example: http://www.wpftutorial.net/CustomLayoutPanel.html
Your custom panel should have X and Y attached dependency properties (like Canvas does) only that methods should use either relative coordinates (0 to 1) instead of (0 to width and 0 to height) or divide by original path width and height to normalize to coordinates.
Related
I want to be able to add icons on top of this canvas that I have, and I want to be able to put these icons at specific x, y coordinates relative to the map. Does anyone know of any quick way I can do this? This is to display event locations exactly where they are on a map.
I have only found the drawImage() method for adding elements on top of a canvas. But is there anyway I could add icons?
I have some problems with implementing autoscrolling in WPF (I think I could call it that way).
I have a canvas placed inside a scrollviwer. On my canvas I can dynamicly add different shapes. The position of this shapes can be changed with mouse. Everytime I add new shape on canvas or change position of shape I fire measureOverride function.Thanks to this scrollview "know" the real size of canvas and the scrollbars appear. However even if scrolbars appear, the view doesn't "follow" shape which I currently move. I mean if I reach visible part of canvas I would like canvas to srcoll.
I was trying to use this function
ScrollToHorizontalOffset()
However I have problem with proper use of that function. I was trying to use (as a parameter) canvas actualwidth but it didn't work well. I also was trying to use as a parameter current position of shape (which I move) but it works only one way. I the viewer follow the moving element if I was moving this element to right side of canvas. However if I move shape back(to the left) the view don't follow the shape.
I hope somebody will understand this :) It is hard to explain my problem.
I also was trying to use as a
parameter current position of shape
That is the correct way to implement. Waht you need is a converter, which will returns the position according to the direction you move the object.
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
In my software (silverlight 3 application) I create a StackPanel in the code, then add objects to it. Is it possible to get its height before rendering? If I try properties like "Height" or "ActualHeight", it's all zeroes...
Thanks!
Have you tried reading the DesiredSize? If that contains 0, try calling Measure passing in a Size structure containing large values for Height and Width, then reading the DesiredSize.
Note that the DesiredSize isn't necessarily what the containing element will allow it have but I suspect it will give you the information you are after.
The panel needs to be rendered before you can get the height back. Then you need to use the ActualHeight property. Height is used to set the desired height of the element.
I don't know of any way to "prerender" elements.
I like to use a grid to position some objects in WPF, because it cares about the position and stuff. In addition, however I also like to be able to draw accross the cells with a Canvas.
I figured out that this can be done by embedding the grid in a canvas. However in order to connect two elements, I need then the absolute position in pixels.. any ideas?
From code you can use the UIElement.TranslatePoint() method.