How to make ellipse circle and resizable keeping its shape? - c#

I have got a Grid, and I want to make a templet a button look like a circle in one of the cells.
I've managed to make it placing it in Canvas but my Circle doesn't resize with the grid. When I used Vertical and Horizontal Alignment Stretch it doesn't work ether.
The question is: How to make a circle in XAML, in one of the cells of the Grid to be resizable keeping it circle shape while I'm resize the window?

If I have understood you, this code works fine:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Ellipse HorizontalAlignment="Stretch" VerticalAlignment="Top"
Height="{Binding ActualWidth, RelativeSource={RelativeSource Self}}"></Ellipse>
</Grid>
The eclipse mantains the shape and it is resize depending on the grid.

Related

XAML How to arrange an element after a centered one

I want to create a button that will display some text together with a status icon inside. The text should be centered and the status icon should go right next to the text. Something like this:
I didn't find a way how to center only the text and then positioning the icon after the centered text. Right now I am using a Grid solution which centers the text and aligns the icon to the right.
<Button VerticalContentAligment="Stretch" HorizontalContentAligment="Stretch">
<Grid>
<TextBlock VerticalAligment="Center" HorizontalAligment="Center"/>
<Image VerticalAligment="Center" HorizontalAligment="Right"/>
</Grid>
</Button>
It's not what I want but it's working for the button's size I have at the moment.
I know that this can be accomplished using some binding magic but it seems too simple for this. It will be great if you can point me to a solution without binding magic but I will grateful even for one with it.
A Grid with 3 columns should work. When the left and right columns have equal width, the middle one is automatically centered.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" VerticalAlignment="Center"/>
<Image Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</Grid>

ActualHeight of Grid not correctly inheriting value from the StackPanel it references

I have a StackPanel with a Grid inside of it. I want that Grid to always be the same size as the StackPanel, so I have the code setup as such:
<StackPanel Name="ContentPanel" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FF2B2B2B" Margin="0,0,0,0">
<Grid HorizontalAlignment="Stretch" Margin="0, 0, 0, 0" VerticalAlignment="Top" Height="{Binding ActualHeight, ElementName=ContentPanel}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="5" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Content Goes Here! -->
</Grid>
</StackPanel>
However, my Grid doesn't seem to be respecting the height. When I resize the Window with the Resize Handle, the grid expands correctly, but doesn't shrink correctly and retains the maximum dimensions. This means anything who's position is referenced via VerticalAlignment=Bottom is liable to 'disappear' from the screen if you resize it to be smaller.
To debug, I gave my StackPanel a set Height of 500 -- Looking at the design preview, I see this:
StackPanel Dimensions:
https://imgur.com/a/Q30Zb
Grid Dimensions:
https://imgur.com/a/oGV4a
Even there, The Grid attempts to fill the space of the entire window, not just it's Parent StackPanel, which it should derive it's height from!
How do I force the Grid to inherent the height of the StackPanel? Alternatively, how do I always force the Grid to consume the entire StackPanel, even if a user resizes the Window?
I've tried changing VerticalAlignment values on all these items, and deleted all my content inside the grid, thinking it might be causing problems, but to no avail!
The stack panel will adjust its height to match the height of its contents. Once the grid has reached a certain size (e.g. 800), the stack panel will not force the grid to become smaller.
One easy option to get what you want would be to put the StackPanel into a parent grid, and then bind the child grid to the parent grid's height.

Left HorizontalAlignment on a Grid

I'm trying to use HorizontalAlignment="Left" in the following situation:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Background="Gray" Text="Small Text" TextWrapping="Wrap"/>
<TextBlock Grid.Column="1" Background="White" Text="This is a very large amount of text" TextWrapping="Wrap"/>
<TextBlock Grid.Column="2" Background="Gray" Text="Medium amount of text" TextWrapping="Wrap"/>
</Grid>
</Window>
My goal is to be able to resize the window, and have the three TextBlocks resize themselves proportionally. This works, but the grid is putting some blank space to the right of the final column, and as I try to resize towards the final column, the columns start to shrink. I want this shrinking behavior, but I don't want it to start until there is no more white space to the right of the rightmost column.
I can't use a UniformGrid as the text lengths can vary, and no other built-in WPF control that I've seen has the ability to resize all children when the parent size changes. I've looked into creating a custom panel, but that seems to be more trouble than it's worth. I feel like something much more simple can be done here.
Any suggestions or ideas are appreciated.
You'll have to build your own custom panel, and handle the case where the AvailableWidth is less then the panel's children DesiredWidth
Panel layout in WPF (Grid is a Panel) is a 2 step process, in the first pass the Panel iterates over its children and provides them with the panel's AvailableWidth. The children respond to this by computing their DesiredWidth.
In the second pass the Panel arranges the children according to their DesiredWidth. In this second pass you have access to the width that all the children (in your case, TextBlocks) require. If it is less than the panel's available width you can compute a percentage to give each one so that they appear to shrink uniformly.
Here's a resource that shows how you can create your own custom panel
What about this?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>

How to set a control's position according to another in windows 8 app?

I have a page like this:
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
here are contents
they are forever absolutely in the center of the screen
no matter of the resolution/size
</StackPanel>
</Grid>
Everything is working fine. But now I want to add a back button in the top-left corner.
And I don't want to split the grid into 2 columns and 2 rows like this:
the contents are no longer absolutely centered, even we can split the grid by percent, because the contents are sometimes very wide and sometimes very narrow.
I want to know how can I keep the contents horizontal/vertical aligned "Center", and then set the back button's position relatively to the content.
I would suggest using a grid layout with 3 columns to ensure the content is centered with the columns widths set to *,Auto, *. This will ensure the main content is always centered and not care about the size of the button. You can then use margins to set the seperation as required. This is a techinique I have used in SL, WPF & Metro.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button HorizontalAlignment="Right" VerticalAlignment="Top" Content="Do Something"/>
<ContentControl VerticalAlignment="Top" Content="My custom content"/>
</Grid>
slightly hacky ansewer
You might be able to achieve by positioning your stackpanel in the center, and then set a negative left margin the width of the button to shift everything left by the required amount...
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="-45,0,0,0">
<button with width and padding totalling 45px />
here are contents
they are forever absolutely in the center of the screen
no matter of the resolution/size
</StackPanel>
</Grid>

How do I automatically resize an image to fit a resized canvas in a silverlight grid cell

For example, I have a grid and one of the cells contains an image. Because I've set the cells background to a colour I can see that the grid is resizing when the user resizes the browser but how do I get the image to resize as well?
I forgot to say that my image is inside a canvas and while the canvas automatically sizes to the grid cell my image does not automatically size to the canvas.
If your Image is placed directly into a grid, its default Stretch behaviour, with is Stretch="Uniform" will ensure that is resizes along with the grid. Try the following XAML to verify:
<Grid x:Name="LayoutRoot" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Source="MyImage.png"
Grid.Row="0"/>
</Grid>
However, based on your updated question ... your image is within a canvas. A Canvas panel is intended to provide absolute positioning of its children. You cannot stretch or align elements within a canvas.
Bottom line - use a different panel as a container!
I have found an answer to this:
<Canvas Name="canvas" >
<Canvas.Background>
<ImageBrush ImageSource="/hsl;component/face-recognition.jpg" />
</Canvas.Background>
</Canvas>

Categories