DataForm commit button is not enabled when data changed.

Posted by Grayson Mitchell on Stack Overflow See other posts from Stack Overflow or by Grayson Mitchell
Published on 2010-03-18T03:53:30Z Indexed on 2010/03/18 4:01 UTC
Read the original article Hit count: 792

Filed under:

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));
                }
            }
        }

© Stack Overflow or respective owner

Related posts about Silverlight