2013-11-28 42 views
0

我在C#如何在WPF中檢索x,y,height,width,text等控件的屬性?

enter image description here

我們怎樣才能檢索到它的座標,並將其保存在文本文件中有限的知識初學者創建窗體時?

+0

協調 –

+0

如果表單包含n個控件,我需要檢索的所有控件的x,y,高度,寬度和文字將其存儲在一個文本文件中單獨行。 – JRU

+1

您需要VisualTreeHelper,遍歷每個Visual控件並獲取其尺寸和位置屬性 http://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper(v=vs.110).aspx , –

回答

0
 private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     // Persist application-scope property to isolated storage 
     IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain(); 
     using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("abc.txt", FileMode.Create, storage)) 
     using (StreamWriter writer = new StreamWriter(stream)) 
     { 
      foreach (var keyValuePair in GetProperties(this)) 
      { 
       writer.WriteLine("{0},{1}", keyValuePair.Key, keyValuePair.Value); 
      } 
     } 
    } 

    private Dictionary<string,string> GetProperties(Window window) 
    { 
     return new Dictionary<string,string>{ 
     {"Left",window.Left.ToString()}, 
     {"Top",window.Top.ToString()}, 
     {"Width",window.Width.ToString()}, 
     {"Height",window.Height.ToString()} 
    }; 
    } 
你需要知道哪些部分
相關問題