使用 min(width, height)/2 作为半径在 WPF 中绘制一个圆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7508398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:06:06  来源:igfitidea点击:

Draw a circle in WPF using min(width, height)/2 as radius

wpfxamlcode-behindgeometry

提问by Aleksandr Vishnyakov

How I can draw a circle in WPF (without code-behind) using min(width, height)/2as radius?

如何使用min(width, height)/2半径在 WPF 中绘制圆(没有代码隐藏)?

回答by Bryan

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

你可以在纯 XAML 中完成,你只需要对值使用绑定。您还必须确保所有内容都已命名

  <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>

回答by paparazzo

Where does width and height come from? Example XAML for a circle is:

宽度和高度从哪里来?圆的示例 XAML 是:

   <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.

圆只是一个高度 = 宽度的椭圆。