2010-03-18 23 views
1

這是一個奇怪的問題。我正在使用數據表單,當我編輯數據時,保存按鈕被啓用,但取消按鈕不是。數據更改時未啓用DataForm提交按鈕

環顧了一下後,我發現我必須實現IEditableObject才能取消編輯。偉大的我做到了(這一切工作),但現在提交按鈕(保存)是灰色的,哈哈。任何人有任何想法,爲什麼提交按鈕不會再激活?

的Xaml

<df:DataForm x:Name="_dataForm"      
      AutoEdit="False" 
      AutoCommit="False" 
      CommandButtonsVisibility="All">      
    <df:DataForm.EditTemplate > 
     <DataTemplate> 
      <StackPanel Name="rootPanel" 
       Orientation="Vertical" 
       df:DataField.IsFieldGroup="True"> 
       <!-- No fields here. They will be added at run-time. -->       
      </StackPanel> 
     </DataTemplate> 
    </df:DataForm.EditTemplate> 
</df:DataForm> 

結合

DataContext = this; 
_dataForm.ItemsSource = _rows; 

...

TextBox textBox = new TextBox();           
Binding binding = new Binding(); 
binding.Path = new PropertyPath("Data"); 
binding.Mode = BindingMode.TwoWay; 
binding.Converter = new RowIndexConverter(); 
binding.ConverterParameter = col.Value.Label; 

textBox.SetBinding(TextBox.TextProperty, binding); 
dataField.Content = textBox; 

// add DataField to layout container 
rootPanel.Children.Add(dataField); 

數據類定義

public class Row : INotifyPropertyChanged , IEditableObject 
     {        
      public void BeginEdit() 
      { 
       foreach (var item in _data) 
       { 
        _cache.Add(item.Key, item.Value); 
       } 
      } 

      public void CancelEdit() 
      { 
       _data.Clear(); 

       foreach (var item in _cache) 
       { 
        _data.Add(item.Key, item.Value); 
       } 

       _cache.Clear(); 

      } 

      public void EndEdit() 
      { 
       _cache.Clear(); 

      } 

      private Dictionary<string, object> _cache = new Dictionary<string, object>(); 

      private Dictionary<string, object> _data = new Dictionary<string, object>(); 

      public object this[string index] 
      { 
       get 
       { 
        return _data[index]; 
       } 
       set 
       { 
        _data[index] = value; 

        OnPropertyChanged("Data"); 
       } 
      } 

      public object Data 
      { 
       get { return this; } 
       set 
       { 
        PropertyValueChange setter = value as PropertyValueChange; 
        _data[setter.PropertyName] = setter.Value; 
       } 
      } 

      public event PropertyChangedEventHandler PropertyChanged; 

      protected void OnPropertyChanged(string property) 
      { 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs(property)); 
       } 
      } 
     } 
+0

我想問題是我添加一個堆棧面板,然後文本框的堆棧面板...這似乎東西的按鈕。 無論如何動態添加控件沒有堆棧面板? – 2010-03-23 04:03:07

回答

0

當您在代碼隱藏中創建字段時,它們實際上只在表單上創建,而不是在「內存」中創建。你可以通過點擊取消按鈕來嘗試這個,當你再次編輯時,表單將是空白的,並且這些字段將需要被重新創建。

所以,當你創建字段時,你需要輸入以下代碼才能真正創建字段。

if (_dataForm.Mode == DataFormMode.Edit) 
       { 
        _dataForm.CommitEdit(); 
        _dataForm.BeginEdit(); 
       } 

當您這樣做時,按鈕將按預期行事。