DataForm commit button is not enabled when data changed

自闭症网瘾萝莉.ら 提交于 2019-12-24 08:16:19

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/2467441/dataform-commit-button-is-not-enabled-when-data-changed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!