none
refresh binding question RRS feed

  • Question

  • Dear All

    I have a WPF DataGrd bound to EF object (MyDocument)

    public string Path { get; set; }
    public string Name { get; set; }
    


    I’ve extended the class by adding partial class in a second file , and add one property

    public string FileFullName
    {
                get
                {
                    if (this.Path != null)
                    {
                        return this.Path + "\\" + this.Name;
                    }
                    return "";
                }
    }
    


    I’ve bound a Column of the DataGrid to  FileFullName

    <DataGridTemplateColumn x:Name="pathColumn"
                                            Header="Path"
                                            Width="300">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Text="{Binding FileFullName}"
                                               HorizontalAlignment="Stretch"
    />                                
                                    <Button Content="..."
                                            Width="100"
                                            Click="OpenFileToAdd"
                                            HorizontalAlignment="Left" />
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    

    The code in the button click event is:

    var openDialog = new OpenFileDialog();
    
                if (openDialog.ShowDialog().Value)
                {
                    var NewDocumentToAdd = new MyDocument()
                    {
                        Name = Path.GetFileName(openDialog.FileName),
                        Path = Path.GetDirectoryName(openDialog.FileName)
                    };
    
                    if (AppLogic.ValidateFileDoublication(dc, NewDocumentToAdd))
                    {
                        SelectedDoc.Path = NewDocumentToAdd.Path;
                        SelectedDoc.Name = NewDocumentToAdd.Name;
                    }
                }
    

    where SelectedDoc is

    private MyDocument SelectedDoc
            {
                get
                {
                    return myDocumentDataGrid.SelectedItem as MyDocument;
                }
            }

    The problem is that the value don’t appear in the grid Column tell I click outside the cell and then click on the cell , how can I fix this ?




    Saturday, December 14, 2013 8:24 AM

Answers

  • If you dont want to modify the automatically generated (?) entity class, you could make the partial class implement the INotifyPropertyChanged interaface and add a method to it that raises the PropertyChanged event:

        public partial class MyDocument : INotifyPropertyChanged
        {
            public string FileFullName
            {
                get
                {
                    if (this.Path != null)
                    {
                        return this.Path + "\\" + this.Name;
                    }
                    return "";
                }
            }
    
            public void Refresh(params string[] propertyNames)
            {
                foreach (string propertyName in propertyNames)
                    NotifyPropertyChanged(propertyName);
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    

    Click event:
    ....

    if (AppLogic.ValidateFileDoublication(dc, NewDocumentToAdd))
                    {
                        SelectedDoc.Path = NewDocumentToAdd.Path;
                        SelectedDoc.Name = NewDocumentToAdd.Name;
          SelectedDoc.Refresh("FileFullName");
                    }

    The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

    • Marked as answer by Hani Safa Saturday, December 14, 2013 3:34 PM
    Saturday, December 14, 2013 1:07 PM
  • You need to implement INotifyPropertyChanged in your MyDocument class so once path for example changed to another value the DataGrid gets notified.
    • Marked as answer by Hani Safa Saturday, December 14, 2013 3:36 PM
    Saturday, December 14, 2013 10:49 AM

All replies

  • You need to implement INotifyPropertyChanged in your MyDocument class so once path for example changed to another value the DataGrid gets notified.
    • Marked as answer by Hani Safa Saturday, December 14, 2013 3:36 PM
    Saturday, December 14, 2013 10:49 AM
  • If you dont want to modify the automatically generated (?) entity class, you could make the partial class implement the INotifyPropertyChanged interaface and add a method to it that raises the PropertyChanged event:

        public partial class MyDocument : INotifyPropertyChanged
        {
            public string FileFullName
            {
                get
                {
                    if (this.Path != null)
                    {
                        return this.Path + "\\" + this.Name;
                    }
                    return "";
                }
            }
    
            public void Refresh(params string[] propertyNames)
            {
                foreach (string propertyName in propertyNames)
                    NotifyPropertyChanged(propertyName);
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    

    Click event:
    ....

    if (AppLogic.ValidateFileDoublication(dc, NewDocumentToAdd))
                    {
                        SelectedDoc.Path = NewDocumentToAdd.Path;
                        SelectedDoc.Name = NewDocumentToAdd.Name;
          SelectedDoc.Refresh("FileFullName");
                    }

    The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

    • Marked as answer by Hani Safa Saturday, December 14, 2013 3:34 PM
    Saturday, December 14, 2013 1:07 PM
  • thanks for your reply ,

    my classes are generated from EF , i'll implement the INotifyPropertyChanged in my partial class

    thanks

    Saturday, December 14, 2013 3:36 PM
  • thanks for your reply ,

    i usually update from the view to my model ( the user edit the value in the DataGrid, the grid update the source ) i forgot about  INotifyPropertyChanged

    thanks again

    Saturday, December 14, 2013 3:43 PM