DataForm commit button is not enabled when data changed
This is a weird problem. I am using a dataform, and when I edit the data the save button is enabled, but the cancel button is not.
After looking around a bit I have found that I have to implement the IEditableObject in order to cancel an edit. Great I did that (and it all works), but now the commit button (Save) is grayed out, lol. Anyone have any idea's why the commit button will not activate any more?
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>
binding
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);
Data Class definition
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));
}
开发者_C百科 }
}
When you create fields in the code-behind they are actually only created on the form and not "in memory". You can try this yourself by clicking on the cancel button, when you edit again the form will be blank, and the fields will need to be recreated.
So when you create the fields you need to put the following code in order to actually create the fields.
if (_dataForm.Mode == DataFormMode.Edit)
{
_dataForm.CommitEdit();
_dataForm.BeginEdit();
}
When you do this the buttons will behave as expected.
精彩评论