简体   繁体   English

C#WPF如何绘制一个圆圈以扩展以填充其父代并在后面的代码中保持一个圆圈

[英]C# WPF how to draw a circle which extends to fill its parent and remains a circle in code behind

I have to draw a circle in a grid. 我必须在网格中画一个圆。 That grid has to adapt proportionally to height and width defined by the Column/Row definition of its parent grid. 该网格必须按比例适应其父网格的“列/行”定义所定义的高度和宽度。 Now if I put stretch it will fill all the space and become an ellipsis while I want it to be a circle. 现在,如果我拉伸,它将填充所有空间并变成省略号,而我希望它是一个圆。

Ok in short the parent grid adapts proportionally like that 简而言之,父网格像这样按比例适应 在此处输入图片说明

then in a routine I add the following code: 然后在例程中添加以下代码:

public void RadialPercentage(Grid grd )
{
    Ellipse elpExt = new Ellipse();
    elpExt.Stroke = Brushes.Green;
    elpExt.StrokeThickness = 4;
    //elpExt.Margin = new Thickness(0);
    //elpExt.HorizontalAlignment = HorizontalAlignment.Center;
    elpExt.VerticalAlignment = VerticalAlignment.Stretch;
    grd.Children.Add(elpExt);

    Ellipse elpInt = new Ellipse();
    elpInt.Stroke = Brushes.Blue;
    elpInt.StrokeThickness = 4;
    elpInt.Margin = new Thickness(20);
    //elpInt.Width = elpInt.Height = dim-20;
    //elpInt.HorizontalAlignment = HorizontalAlignment.Center;
    elpInt.VerticalAlignment = VerticalAlignment.Stretch;
    grd.Children.Add(elpInt);
    return;
}

but the effect is the following: 但是效果如下:

在此处输入图片说明

so it stretches both vertically and horizontally even if I only put the vertical and not the horizontal constraint. 因此即使我只放置垂直约束而不是水平约束,它也会在垂直和水平方向上拉伸。 If I set it to center the ellipse collapses. 如果将其设置为居中,则椭圆会崩溃。

To solve the problem even I am not sure that this is the right thing to do I tried to take a look of the weight/heigth of the parent grid but obviously both those values and the actual values are set to zero. 为了解决该问题,即使我不确定这样做是否正确,我也尝试查看父网格的权重/高度,但显然这些值和实际值都设置为零。

thanks for helping Patrick 感谢您对Patrick的帮助

What about setting Width 's binding to ActualHeight of the ellipse and set HorizontalAlignment to Center ? 如何将Width的绑定设置为椭圆的ActualHeight并将HorizontalAlignment设置为Center呢? Something like this: 像这样:

var ellipse = new Ellipse();

var binding = new Binding(Ellipse.ActualHeightProperty.Name)
{
    RelativeSource = new RelativeSource(RelativeSourceMode.Self),
    Mode = BindingMode.OneWay
};

ellipse.VerticalAlignment = VerticalAlignment.Stretch;
ellipse.HorizontalAlignment = HorizontalAlignment.Center;

BindingOperations.SetBinding(ellipse, Ellipse.WidthProperty, binding);

You can update the size of your Ellipse each time the parent Grid is resized. 每次调整父Grid的大小时,您都可以更新Ellipse大小。

You should add to your Grid the SizeChanged Event . 您应该将SizeChanged Event添加到Grid中。 XAML example: XAML示例:

<Grid Name        = "MyGrid"
      SizeChanged = "MyGridSizeChanged">
  <!-- rows and columns definitions -->
  <Ellipse Name        = "MyEllipse"
           Grid.Row    = "i"
           Grid.Column = "j" />
</Grid>

Now, each time the Grid is resized the function MyGridSizeChanged will executed. 现在,每次调整Grid大小时,都会执行MyGridSizeChanged函数。 You should add into it code, which set sizes of your Ellipse equal to smallest side of contained cell. 您应该在其中添加代码,该代码将Ellipse大小设置为等于所包含单元格的最小边。 C# example: C#示例:

void MyGridSizeChanged(object sender, SizeChangedEventArgs e) {
    if (sender is Grid myGrid) {
        var cellHeight = myGrid.RowDefinitions[Grid.GetRow(MyEllipse)].ActualHeight;
        var cellWidth  = myGrid.ColumnDefinitions[Grid.GetColumn(MyEllipse)].ActualWidth;
        var newSize    = Math.Min(cellHeight, cellWidth);

        MyEllipse.Height = newSize;
        MyEllipse.Width  = newSize;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM