2013-10-31 25 views
1

我可能失去了一些東西很明顯,但我沒有看到它。任何建議表示讚賞!我的數據對象如何拒絕來自數據綁定控件的更改? (Control未顯示實際值)

我在我的DO一個屬性(表示硬件設備的數據對象),其被結合到控制(組合框,裝訂「文本」屬性)在我的形式。如果用戶鍵入一個無效值,我的屬性的Set {}會拒絕更改並將相應的私有值設置爲有效。

如果公共屬性,都會通過控制讀取,它會讀取正確的價值,但它沒有這樣做。它繼續顯示用戶輸入的無效值。

我不認爲我可以使用常規的數據綁定「驗證」,因爲控件本身,而這些表的工作是不知道什麼樣的價值限度應該是(我錯了?)。只有數據對象知道其限制(可以根據所選的硬件設備版本,選擇的度量單位等進行更改)。

我想我可以使用蒙面文本框並將其最小/最大值綁定到數據對象中的另外兩個屬性,但這似乎很麻煩,我想使用組合框,以便用戶可以選擇常用值而不是始終打字。

我怎樣才能得到我的控制它嘗試更新DO屬性後刷新它的價值?

形式:

comboBox_Speed.DataBindings.Add("Text", testProgramObject, "SpeedSetting", true, DataSourceUpdateMode.OnPropertyChanged); 
    comboBox_Speed.DataBindings["Text"].ControlUpdateMode = ControlUpdateMode.OnPropertyChanged; 
    comboBox_Speed.DataBindings["Text"].Format += new ConvertEventHandler(DeviceClass.DisplayInSelectedUnits); 
    comboBox_Speed.DataBindings["Text"].Parse += new ConvertEventHandler(DeviceClass.StoreInDatabaseUnits); 

在數據對象:

private UInt16 _speedSetting = 0; 
    public Double SpeedSetting{ 
     get { return _speedSetting ; } 
     set 
     { 
      double temp = value; //databinding Parse function requires type Double for its destination. 
      try { _speedSetting = Convert.ToUInt16(temp); } // maybe user typed number too big for UInt16 
      catch 
      { 
       //_speedSetting= 0; // <-- Does not cause Control to display this value. It keeps invalid value. 
       SpeedSetting= 0; // <-- I thought this would trigger the control to read the changed value, but it doesn't. 
      } 
      NotifyPropertyChanged("SpeedSetting"); 
     } 
    } 

謝謝!!!!

回答

0

嘗試TextUpdate事件。

comboBox_Speed.TextUpdate += new EventHandler(comboBox_Speed_TextUpdate); 

void comboBox_Speed_TextUpdate(object sender, EventArgs e) 
    { 
     comboBox_Speed.DataBindings["Text"].WriteValue(); 
     comboBox_Speed.DataBindings["Text"].ReadValue(); 
    } 
+0

雖然這個工作,並立即這樣做,它會導致另一個問題。每次讀取屬性值(每次用戶更改文本時),光標都會重置爲位置0。這會導致數字顯示爲向後鍵入。 – XKCD137

+0

然後我嘗試在寫入/讀取之前保存光標位置,但是如果添加了一個數字,則光標現在位於其左側,而不是右側。 接下來,我試圖保持光標在右側,這將讓用戶鍵入數字,但他們無法從任何其他位置適當編輯,和定位從右到左的時候,這將是一場噩夢語言。 所以最後,我只是使用OnValidated來檢查並重新讀取值。 – XKCD137

0

謝謝,第二杯咖啡......

這裏有一個解決方案,但是那裏有它運行實時的方式?(用戶鍵入的內容),以便他們可以立即看到錯誤的方式?

這種技術更新控制的顯示驗證後(當焦點從控制移開),

我用ComboBox.Validated事件來觸發控制重新讀取其從屬性值。

補充:

comboBox_Speed.Validated += new EventHandler(comboBox_Speed_Validated); 

和:

private void comboBox_Speed_Validated(object sender, System.EventArgs e) 
{ 
    comboBox_Speed.DataBindings["Text"].ReadValue(); 
} 
相關問題