2016-05-17 134 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> 

這將拿起所有從以前的風格等屬性,但改變其高度(或任何其他財產,你可能需要改變)

相關問題