How I can draw a circle in WPF (without code-behind) using min(width, height)/2
as radius?
Asked
Active
Viewed 2.6k times
12

Dan Is Fiddling By Firelight
- 5,981
- 17
- 77
- 128

Aleksandr Vishnyakov
- 1,872
- 5
- 23
- 39
-
1Your assumption that this is possible without code-behind is probably wrong. – H.B. Sep 22 '11 at 01:07
-
I writing theme for WPF and I need to solution without using code-behind =( – Aleksandr Vishnyakov Sep 22 '11 at 01:10
-
use an ellipse control and the height and width values will be equal then it will be an circle – ravithejag Aug 09 '12 at 06:16
2 Answers
11
you can do it in pure XAML you just need to use Binding for the values. You also have to make sure that everything is named
<Grid Name="grdMain">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75" Name="Col1" />
<ColumnDefinition Width="100" Name="Col2" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="75" Name="Row1" />
<RowDefinition Height="100" Name="Row2" />
</Grid.RowDefinitions>
<Ellipse Grid.Column="1" Grid.Row="1"
Canvas.Top="50"
Canvas.Left="50"
Fill="#FFFFFF00"
Height="{Binding RowDefinitions/ActualHeight, ElementName=Row1, Mode=OneWay}"
Width="{Binding ColumnDefinitions/ActualWidth, ElementName=Col1, Mode=OneWay}"
StrokeThickness="5"
Stroke="#FF0000FF"/>
</Grid>

Bryan
- 357
- 4
- 16
8
Where does width and height come from? Example XAML for a circle is:
<Canvas Background="LightGray">
<Ellipse
Canvas.Top="50"
Canvas.Left="50"
Fill="#FFFFFF00"
Height="75"
Width="75"
StrokeThickness="5"
Stroke="#FF0000FF"/>
</Canvas>
A circle is just an Ellipse where Height = Width.

paparazzo
- 44,497
- 23
- 105
- 176
-
Ellipse placed in Grid. Grid determine width and heigh automatically (Column Widdth="*", Row Height="*") – Aleksandr Vishnyakov Sep 22 '11 at 16:48
-
I don't think you can do that in pure XAML as I don't know of any logic to get the minimum of two numbers. But I am not a XAML expert. – paparazzo Sep 22 '11 at 22:03