2016-05-17 148 views
0

我有一个椭圆形的“width ='auto'”,我想要一个圆,所以不可能设置“height ='auto'”,因为如果用户调整大小窗口,圆形将是一个椭圆。我试过“Height ='{Binding ElementName = TheLeft,Path = Width}'”。将一个动态元素的值赋给另一个元素

<Page 
    x:Class="App2.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:App2" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <Grid Background="White"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="3*" /> 
      <ColumnDefinition Width="1*" /> 
      <ColumnDefinition Width="3*" /> 
     </Grid.ColumnDefinitions> 
     <!--<Ellipse x:Name="TheLeft" Fill="Pink" Grid.Column="0" Height="{Binding ElementName=TheLeft, Path=Width}" Width="auto" VerticalAlignment="Bottom"/>--> 
     <!-- I've set Height="200" in the uncommented element --> 
     <Ellipse x:Name="TheLeft" Fill="Pink" Grid.Column="0" Height="200" Width="auto" VerticalAlignment="Bottom"/> 

     <Rectangle Fill="Red" Grid.Column="1" RadiusX="50" RadiusY="40"/> 
     <Ellipse Fill="Pink" Grid.Column="2" Height="200" Width="auto" VerticalAlignment="Bottom"/> 
    </Grid> 
</Page> 

回答

0

首先,我肯定会建议您去掉内联样式,特别是因为您需要为两个椭圆的许多属性使用相同的值。

<Style x:Key="circularEllipse" TargetType="{x:Type Ellipse"}> 
    <Setter Property="Fill" Value="Pink"/> 
    <Setter Property="VerticalAlignment" Value="Bottom"/> 
    <Setter Property="Height" Value="200"/> 
    <Setter Property="Width" Value="{Binding Path=Height, RelativeSource={RelativeSource Self}}"/> 
</Style> 

然后当你的代码椭圆 - 你只需要调用这个风格,它可以用来对他们俩的,节省您的编码时间,并减少对布局的代码量。

<Ellipse x:Name="TheLeft" Grid.Column="0" Style="{StaticResouce circularEllipse}"/> 

所以,如果你需要改变圆的大小,你只需要在一个地方改变属性。此外,如果您需要变体,则可以使用BasedOn选项,而不必重做整个代码。如:

<Style x:Key="circularEllipse2" TargetType="{x:Type Ellipse}" BasedOn="{StaticResource circularEllipse}"> 
    <Setter Property="Height" Value="100"/> 
</Style> 

这将拿起所有从以前的风格等属性,但改变其高度(或任何其他财产,你可能需要改变)

相关问题