电竞比分网-中国电竞赛事及体育赛事平台

分享

WPF中獲取動(dòng)態(tài)添加控件的ActualHeight和相對(duì)坐標(biāo)

 xyjackxjw 2013-05-14

WPF中獲取動(dòng)態(tài)添加控件的ActualHeight和相對(duì)坐標(biāo)

分類(lèi): WPF1526人閱讀評(píng)論(1)收藏舉報(bào)

      在WPF中有時(shí)會(huì)用到獲取控件的ActualHeight,從而進(jìn)行相關(guān)運(yùn)算,我是需要在一個(gè)UniformGrid中動(dòng)態(tài)的添加Button,然后獲取 Button的ActualHeight和相對(duì)于UniformGrid的坐標(biāo)。測(cè)試項(xiàng)目代碼如下(VS2010):
    XAML部分:

  1. <Window x:Class="WpfApplication2.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="450" Width="525">  
  5.     <Grid>  
  6.         <Grid.ColumnDefinitions>  
  7.             <ColumnDefinition/>  
  8.         </Grid.ColumnDefinitions>  
  9.         <Grid.RowDefinitions>  
  10.             <RowDefinition Height="*"/>  
  11.             <RowDefinition Height="5*"/>  
  12.         </Grid.RowDefinitions>  
  13.         <Button Name="btnTest" Grid.Column="0" Grid.Row="0" Height="50" Click="btnTest_Click">TestHeight</Button>  
  14.         <UniformGrid Name="ufg" Height="300" Grid.Row="1" Margin="0,21">  
  15.         </UniformGrid>  
  16.     </Grid>  
  17. </Window>   

     C#部分:

[c-sharp] view plaincopy?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14. namespace WpfApplication2  
  15. {  
  16.     /// <summary>  
  17.     /// Interaction logic for MainWindow.xaml  
  18.     /// </summary>  
  19.     public partial class MainWindow : Window  
  20.     {  
  21.         public MainWindow()  
  22.         {  
  23.             InitializeComponent();  
  24.         }  
  25.         private void btnTest_Click(object sender, RoutedEventArgs e)  
  26.         {  
  27.             this.ufg.Children.Clear();  
  28.             for (int i = 0; i < 20; i++)  
  29.             {  
  30.                 Button btn = new Button();  
  31.                 btn.Content = i.ToString();  
  32.                 btn.Click += new RoutedEventHandler(btn_Click);  
  33.                 this.ufg.Children.Add(btn);  
  34.             }  
  35.             Button b;  
  36.             GeneralTransform transform;  
  37.             Point thePoint;  
  38.             b = ufg.Children[10] as Button;//b的實(shí)際高度是60  
  39.             MessageBox.Show("Before UpdateUniformGrid Children[10].ActualHeight:{0}",  
  40.                              b.ActualHeight.ToString());  
  41.             transform = b.TransformToAncestor(ufg);  
  42.             thePoint = transform.Transform(new Point(0, 0));  
  43.             MessageBox.Show("Before UpdateUniformGrid the Point.Y relative to UniformGrid of Children[10](TransformToAncestor): {0}",  
  44.                             thePoint.Y.ToString());  
  45.             MessageBox.Show("Before UpdateUniformGrid the Point.Y relative to UniformGrid of Children[10](TranslatePoint):{0}",  
  46.                             b.TranslatePoint(new Point(0, 0), ufg).Y.ToString());  
  47.             ufg.UpdateLayout();//更新UniformGrid的界面,為獲取ActualHeight很重要,但是獲取相對(duì)坐標(biāo)倒是可以沒(méi)有  
  48.             MessageBox.Show("After UpdateUniformGrid Children[10].ActualHeight:{0}",  
  49.                             b.ActualHeight.ToString());  
  50.             transform = b.TransformToAncestor(ufg);  
  51.             thePoint = transform.Transform(new Point(0, 0));  
  52.             MessageBox.Show("After UpdateUniformGrid, the Point.Y relative to UniformGrid of Children[10](TransformToAncestor):{0}" ,  
  53.                             thePoint.Y.ToString());  
  54.             MessageBox.Show("After UpdateUniformGrid, the Point.Y relative to UniformGrid of Children[10](TranslatePoint):{0}",  
  55.                             b.TranslatePoint(new Point(0, 0), ufg).Y.ToString());  
  56.         }  
  57.         //UniformGrid中Button單擊事件  
  58.         private void btn_Click(object sender, EventArgs e)  
  59.         {  
  60.             MessageBox.Show((sender as Button).ActualHeight.ToString());  
  61.             Button b = sender as Button;  
  62.             MessageBox.Show("the Point.Y relative to UniformGrid:{0}",  
  63.                             b.TranslatePoint(new Point(0, 0), ufg).Y.ToString());  
  64.         }  
  65.   
  66.     }  
  67. }  

       運(yùn)行一下測(cè)試項(xiàng)目就會(huì)發(fā)現(xiàn)在UpdataLayout之前是得不到ActualHeight 的,但是可以得到相對(duì)坐標(biāo),而在UpdataLayout之后就可以得到ActualHeight和相對(duì)坐標(biāo)。網(wǎng)上說(shuō)WPF控件需要在Render之后才能得到ActualHeight,而UpdataLayout應(yīng)該就有這個(gè)功能吧,初接觸WPF,所以具體的原理還不太清楚。

      另外一個(gè)是在獲取相對(duì)坐標(biāo)的時(shí),在for循環(huán)外的btnTest_Click事件中和在UniformGrid中的按鈕單擊事件中,使用TransformToAncestor,或TranslatePoint的方法才能得到,其實(shí)獲取控件的相對(duì)坐標(biāo)還有其他方法,在此不一一列出,需要的朋友可以Google一下。

附件下載(2010項(xiàng)目)

WPF中提供了多種布局方式,因此在布局中的定位相對(duì)于WinForm的絕對(duì)定位要靈活的多,在WPF中,控件均沒(méi)有如WinForm中的Location屬性,但是,對(duì)應(yīng)的提供了各種設(shè)定與獲取相對(duì)于承載元素的定位

一般來(lái)說(shuō),Wpf中的布局控件大多都是相對(duì)定位(網(wǎng)格,流式,面板等),如果我們要改變控件在布局中的位置可以用Margin,Padding等類(lèi)似HTML中的方式,雖然說(shuō)這種方式在WinForm也可用,但是WPF中的布局方式與靈活性已經(jīng)更接近與HTML了

WPF中也保留了相對(duì)傳統(tǒng)的布局方式,如在Canvas容器中可以用SetLeft(),SetTop()來(lái)絕對(duì)定位

關(guān)于控件定位詳細(xì)具體可參考 http://msdn.microsoft.com/zh-cn/library/ms751709.aspx

下來(lái)我們來(lái)簡(jiǎn)單描述幾種獲取控件位置的方式,這也是很多新手容易糾結(jié)的地方

1.獲取鼠標(biāo)在控件中的坐標(biāo)

復(fù)制代碼
1  //在Mouse相關(guān)的事件中的方式  
2   void item_MouseDown(object sender, MouseButtonEventArgs e)
3    {
4     Point point = e.GetPosition(canvas); 
5   } 
6 
7   //或者M(jìn)ouse的靜態(tài)方法GetPosition() 獲取與指定元素相對(duì)的鼠標(biāo)位置=>等同于上面 
8     Point point = Mouse.GetPosition(canvas);
復(fù)制代碼

 

2.獲取控件相對(duì)于另一控件的坐標(biāo)

//將相對(duì)于此元素的某個(gè)點(diǎn)轉(zhuǎn)換至相對(duì)于指定元素的坐標(biāo)中
void item_MouseDown(object sender, MouseButtonEventArgs e)
 {
      Rectangle rectangle =sender as Rectangle; 
    Point point = rectangle.TranslatePoint(new Point(),canvas); 
 }

<strong>//將相對(duì)于此元素的某個(gè)點(diǎn)轉(zhuǎn)換至相對(duì)于指定元素的坐標(biāo)中
void item_MouseDown(object sender, MouseButtonEventArgs e)
{
Rectangle rectangle =sender as Rectangle;
   Point point = rectangle.TranslatePoint(new Point(),canvas);
}
</strong>

  

3.獲取控件在Window中的坐標(biāo)

Window window =  Window.GetWindow(canvas);
Point  point  =  canvas.TransformToAncestor(window).Transform(new Point(0, 0));

另外,c#中還提供了控件坐標(biāo)與屏幕坐標(biāo)之間的轉(zhuǎn)換,PointFromScreen,PointToScreen,這些就不再一一說(shuō)明了

總之,根據(jù)實(shí)際情況選擇最適合的方式來(lái)獲取控件坐標(biāo)或定位Posts - 3 Articles - 0 Comments - 0

WPF獲取某控件的位置,也就是偏移量

此段示例在MSDN中可見(jiàn)。XAML代碼如下:

復(fù)制代碼
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <StackPanel Margin="16">
    <StackPanel Margin="8">
      <TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" />
    </StackPanel>
  </StackPanel>
</Window>
復(fù)制代碼

1、如果只需要獲取相對(duì)于其父級(jí)的偏移量,則可以使用以下方法:

// Return the offset vector for the TextBlock object.
Vector vector = VisualTreeHelper.GetOffset(myTextBlock);

// Convert the vector to a point value.
Point currentPoint = new Point(vector.X, vector.Y);

偏移量保存在Vector對(duì)象中

2、相對(duì)靈活的方法可以使用 TransformToAncestor方法,這樣可以獲得相對(duì)于Window的偏移量

// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myTextBlock.TransformToAncestor(this);

// Retrieve the point value relative to the parent.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多