2012-10-04 211 views
0

我是WPF的初學者,必須爲某人的用戶界面添加功能。這是代碼的一部分。如何獲取邊界的邊界值?

<Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" > 
     <Grid Grid.Row="0"> 
      <Grid.RowDefinitions> 
       <RowDefinition></RowDefinition> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition></ColumnDefinition> 
       <ColumnDefinition Width="{Binding ElementName=passFailIndicator, Path=Width}"></ColumnDefinition> 
      </Grid.ColumnDefinitions> 
      <Border Background="Black" local:StretchPanel.Proportion="1" Name="imageBorder" Grid.Column="0"> 
       <Border BorderThickness="1" Margin="2" BorderBrush="Green" HorizontalAlignment="Left" VerticalAlignment="Top" Width="{Binding ElementName=imageBorder, Path=Width}"> 
        <Grid Width="{Binding ElementName=imageBorder, Path=Width}"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition/> 
          <ColumnDefinition Width="{Binding ElementName=imageSelectExpander, Path=Width}"/> 
         </Grid.ColumnDefinitions> 
         <Image Grid.Column="0" x:Name="image" Source="{Binding DisplayImage, Mode=OneWay}" VerticalAlignment="Top" Stretch="Uniform" HorizontalAlignment="Left" StretchDirection="Both" MouseMove="image_MouseMove" /> 
         <TextBlock Name="pxPos" Text="mouse position" HorizontalAlignment="Right" VerticalAlignment="Bottom" MaxHeight="20" Foreground="Aqua"></TextBlock> 
         <Expander Grid.Column="1" VerticalAlignment="Top" Name="imageSelectExpander"> 
          <ComboBox x:Name="imageSelect" ItemsSource="{Binding AvailableImages, Mode=OneWay}" SelectedIndex="{Binding ImageSelect, Mode=TwoWay}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="4" Padding="4" MaxHeight="40"></ComboBox> 
         </Expander> 
        </Grid> 
       </Border> 
      </Border> 
      <Border x:Name="passFailIndicator" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100" Height="100"> 
       <Viewbox Stretch="Uniform" > 
        <Path Style="{Binding TestResult, Mode=OneWay, Converter={StaticResource testResultToPathStyle}}" Margin="2" /> 
       </Viewbox> 
      </Border> 
     </Grid> 
    </Border> 

我試圖訪問線10分的保證金=「2」當我使用this.imageBorder.Margin,我得到{0,0,0,0}。我將如何獲得2(當然,這個值可能會改變)?

感謝

編輯:

FrameworkElement fe = (FrameworkElement)this.imageBorder.Child; 
      pxPos.Text = (string.Format("x:{0} y:{1}", (int)((double)(pt.X - fe.Margin.Left) * (double)this.image.Source.Width/this.image.ActualWidth), (int)((double)(pt.Y - fe.Margin.Top)*(double)this.image.Source.Height/this.image.ActualHeight))); 

回答

4

邊境標記imageBorder沒有它Margin屬性設置爲任何東西,所以你得到默認Margin,這是0

要獲得邊界內的邊界的邊界屬性,可以給它一個x:Name,以便您可以在後面的代碼中訪問它,或者查看imageBorder.Child屬性以查找chi ld邊界對象,然後將其轉換爲FrameworkElement以獲得其邊距屬性。

至於你爲什麼得到{0,0,0,0}Margin屬性的類型爲Thickness,其中包括左,上,右,底的屬性。如果您將保證金設置爲單個值(如2),則會自動將其轉換爲Thickness對象,並將其所有4個屬性設置爲2.

+0

謝謝,我會明確寫下代碼以防其他人一天尋找它 – Denis