2015-02-09 26 views
1

我已經習慣了數據綁定在 WPF其中有一個完整的支持它,我知道它在Windows窗體存在,但非常有限約束力的形式的大小和位置屬性標籤Text屬性

只是出於好奇,我想做一些很基本的數據綁定:

  • 結合當前Form的一個LabelText財產
  • Size屬性爲Location財產做同樣的

是否有可能,如果是的話如何實現它?

+0

你見過這個:http://stackoverflow.com/questions/5032194/binding-in-winform-like-in-wpf? – 2015-02-09 20:09:19

+0

您的第一個綁定應該使用「位置」作爲數據成員名稱而不是「位置」。如果你修復這個錯誤,那麼它應該可以工作。 – RogerN 2015-02-09 20:09:26

+0

@RogerN Ouch太愚蠢了:)但爲什麼它也打破了第二個綁定呢?謝謝。 – Pragmateek 2015-02-09 20:16:42

回答

0

這裏是你如何實現它:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     Label positionOutput = new Label { Dock = DockStyle.Top }; 
     positionOutput.DataBindings.Add("Text", this, "Location"); 

     Label sizeOutput = new Label { Dock = DockStyle.Top }; 
     sizeOutput.DataBindings.Add("Text", this, "Size"); 

     this.Controls.Add(positionOutput); 
     this.Controls.Add(sizeOutput); 
    } 
} 

它與LocationSize,因爲無論是Form類提供專用的數據綁定事件:LocationChangedSizeChanged

但與WidthHeight由於沒有WidthChangedHeightChanged事件,您不會受益於實時自動更新。