Patrick
Patrick

Reputation: 2583

How to cope with a observable collection binded datagrid with manual update

I have a datagrid

<DataGrid Name="dtgFeatures" Height="100" Margin="10" ColumnWidth="*" CanUserAddRows="True" MouseLeftButtonUp="DtgFeatures_MouseLeftButtonUp"/>

which is binded to an observable collection

ObservableCollection<CfgPartPrograms> obcCfgPartPrograms = new ObservableCollection<CfgPartPrograms>();

with

[Serializable]
public class CfgPartPrograms
{
    public CfgPartPrograms() { }
    public string Group{ get; set;}
    public string Description{ get; set;}
    public string Filename{ get; set;}<------set with openfiledialog
    public string Notes{ get; set;}
}

Now since I want to be able to insert the filename with an openfileDialog I have add this code:

private void DtgFeatures_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    int column = (sender as DataGrid).CurrentCell.Column.DisplayIndex;
    if ( column == 2)
    {
        OpenFileDialog ofdPP = new OpenFileDialog();
        if (ofdPP.ShowDialog() == true)
        {
            if (obcCfgPartPrograms.Count == 0)
                obcCfgPartPrograms.Add(new CfgPartPrograms() { Filename = ofdPP.FileName });
            else
                obcCfgPartPrograms[selectedIndex].Filename = ofdPP.FileName;
            dtgFeatures.ItemsSource = null;
            dtgFeatures.ItemsSource = obcCfgPartPrograms;
        }
    }

the problem is that when I set the filename the observable collection has not been updated yet. I'll explain that with images:

enter image description here

So I have added aaaa and bbb now I want to force the filename with the code above but when I do that the bind action has not been done yet on the observable collection so that aaaa and bbbb are not present.

enter image description here

In short how to tell the binded datagrid to update the binding??

Thanks in advance Patrick

Upvotes: -1

Views: 57

Answers (2)

Patrick
Patrick

Reputation: 2583

I found it here: I was missing the

dtgFeatures.CommitEdit(DataGridEditingUnit.Row, true);

command

Upvotes: 0

mm8
mm8

Reputation: 169200

Your CfgPartPrograms class should implement the INotifyPropertyChanged interface and raise the PropertyChanged event whenever a data bound property is set to a new value: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

[Serializable]
public class CfgPartPrograms : System.ComponentModel.INotifyPropertyChanged
{
    public CfgPartPrograms() { }

    public string Group { get; set; }
    public string Description { get; set; }
    private string _fileName;

    public string Filename
    {
        get { return _fileName; }
        set { _fileName = value; NotifyPropertyChanged(); }
    }

    public string Notes { get; set; }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
    }
}

Upvotes: 1

Related Questions