WPF Ellipse why can I not add a grid? - wpf

I have a WPF application. I am trying to create a ellipse toggle button. I wanted to have a textbox in the ellipse with two images next to each other below the textbox. In my other buttons I have added a grid with two rows to achieve this.
However it doesn't like it when I try to add a grid to an ellipse. "The type ellipse does not support direct content". What does this mean? Is it possible to do what I wish?

This particular error really couldn't be too much clearer:
The type Ellipse does not support direct content.
The Ellipse Shape is not a ContentControl. Therefore, it does not have a Content property. This means that you cannot add any content to it.
What you can do is to add a Grid with no columns or rows defined and add your Ellipse into the Grid. Then you can overlay other UI elements in front of the Ellipse, which may appear as if they were inside it.

Related

How to create a UserControl with an irregular shape?

In my Silverlight 4 application I need to create a user control with an irregular shape. The "main display" of the UC is a standard rectangle but I need to have tabs (simple text blocks, where the user can click) that are outside of the main display rectangle.
Is this possible with Silverlight 4? If so, how?
Thanks in advance.
You can position elements of a control outside its normal layout in a number of ways. You could use Canvas but if most of the control is standard Grid rectangle then you can use a Grid. The trick is to use negative Margins.
<Grid x:Name="LayoutRoot">
<Border Margin="0 -22 0 0">
<TextBlock Text="I appear above the UserControl layout" />
</Border>
</Grid>
Note that if the Usercontrol is being used as the Visual root then this won't work because the Silverlight plugin will not render beyound its client rectangle.
It is, you can have transparent background behind the tabs which can let clicks through, effectively behaving as if the shape was different. The UserControl will still have a rectangular shape including the tabs, unless you wrap then into a Popup and float out of the UC with some offset.
Technically, you can have elements outside the UserControl's rectangle if you use a Canvas for your LayoutRoot instead of a Grid. Elements in a Canvas aren't clipped to the canvas size. I wouldn't recommend this, however, because you won't be able to use Margin to size and align your controls inside it. It would be better to have all child controls inside a Grid LayoutRoot.
Which brings us to the question of irregularity. If you want to 'see through' parts of the control and be able to click through them (i.e. click objects underneath it), all you need to do is keep the UserControl's and the LayoutRoot's Background to null or just not set it at all. Wherever there is a lack of any background, clicks will go through. Note that if you set the background to Transparent it will make the control behave as a rectangle (as if it's filled with solid color) with respect to mouse input.
Another thing is if you want to see HTML controls under the see-through parts of your app. Then, you'll have to use windowless mode, but that's another can of worms.

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

WPF, ShowGridLines equivalent for wrap panel

I need to display a 1 pixel wide border around all wrap panel cells, kinda like excel grid. Unfortunately the wrap panel does not implement the grid ShowGridLines property. I can't put a border inside every cell because adjacent cells will have a 2 pixel border instead of 1 pixel.
Since the wrap panel arranges it's layout dynamically and does not expose it's properties I can't evaluate the correct value for a border inside a cell. Any workaround possible?
There is no way to show grid lines because it's just not a grid. Even in a WPF grid control, you have very little control over the appearance of lines: How can I change the color of the gridlines of a Grid in WPF?. They're really just for debugging. The workaround is to put a border inside each cell that only has thickness on the bottom and on the right: BorderThickness="0,0,1,1"

How to draw lines across cells in a WPF Grid

I am trying to fill the Grid control with custom controls dynamically. After the grid is filled with elements, I need to connect these elements by lines. How do I achieve this....
Thanks in advance
I have an idea you could try: Within a Grid, items will overlap each other. So you could create an outer Grid which contains your grid and also a Canvas with a transparent background. On the Canvas, you could draw lines which would look like they are connecting your Grid's elements.
You can use a Grid set as an ItemsPanel for an ItemsControl. It's not quite easy, but if that's the way you want to go.
For connecting elements by a line, here is a good article that shows you just that:
UI Frontiers: Thinking Outside the Grid (by Charles Petzold)

Anchor in WinForms==Horizontal(+Vertical)Alignment in WPF?

Can't find anchor property in WPF, was it reconstructed to Horizontal and Vertical Alignments?
Is it the same?
I wanted to stretch my control in both sides (right and left), but it worked rather different than anchor
You can do something like Anchor like this
<...HorizontalAlignment='Stretch' VerticalAlignment='Stretch' Margin='50,50,50,50'.../>
The Anchor and Dock properties of Windows Forms are replaced by appropriate layouting containers in WPF. Depending on what exactly you need you should be able to create your desired layout in WPF with the Grid or DockPanel containers.
There are small white circles at the edges of the control. Click them, and they will be converted to triangles which mean the edge is anchored.

Resources