Datagrid. I Can't refresh field

断了今生、忘了曾经 提交于 2019-12-24 04:18:41

问题


I can't refresh a field in my datagrid.

This is my datagrid

 <DataGrid 
    ItemsSource="{Binding Dati_Viaggio}" SelectedItem="{Binding SelectDati_Viaggio}" 
       Style="{DynamicResource ST_DataGrid}" 
        CellStyle="{DynamicResource St_DataGridCellStyle}" SelectionMode="Single"  Name="Dg_Dati" 
        IsReadOnly="True" RowDetailsVisibilityMode="VisibleWhenSelected" >

this is the filed in datagrid that I want to refresh

 <DataGridTextColumn x:Name="col_NumOrd" Binding="{Binding Path=NumOrd}"  Header="Num. Ord."  Width="150" />

And this is the property.

public ObservableCollection<Model_Ricerca_Dati_Viaggio> Dati_Viaggio 
   { get; set; }

   private Model_Ricerca_Dati_Viaggio _SelectDati_Viaggio;
   public Model_Ricerca_Dati_Viaggio SelectDati_Viaggio {
get { return _SelectDati_Viaggio; }
set {
    _SelectDati_Viaggio = value;
    OnPropertyChanged("SelectDati_Viaggio");}

Why if i write this code the datagrid doesn't refresh the field ?

SelectDati_Viaggio.NumOrd= "abcabc";
OnPropertyChanged("SelectDati_Viaggio");

Thank you


回答1:


Try to refresh binding by redeclaring and re-query the collections and initialize to the datagrid, just tried this after several days of searching of solutions. If you get the idea.

server = mongoClient.GetServer();
        database = server.GetDatabase("facultyDataAndSchedule");
        collection = database.GetCollection<facultyData>("faculty");
        var query = collection.FindAllAs<facultyData>().SetFields(Fields.Include("facultyID", "term", "acadYear", "age", "program", "lastName", "firstName", "middleName", "dateOfBirth", "rank", "yearsOfTeachingS", "yearsOfTeachingO", "status", "services"));
        // List<facultyData> resultList = query.ToList<facultyData>();
       resultBinding = new ObservableCollection<facultyData>(query);
       facultyDataGrid.ItemsSource = resultBinding;
        try
        {

            try
            {
                var entity = new facultyData
                {
                    facultyID = facultyID_Textbox.Text.ToString(),
                    term = termComboBox.SelectedItem.ToString(),
                    age = int.Parse(age_TextBox.Text),
                    acadYear = "2014-2015",
                    firstName = firstName_TextBox.Text.ToString(),
                    lastName = lastName_TextBox.Text.ToString(),
                    middleName = middleName_TextBox.Text.ToString(),
                    dateOfBirth = dateOfBirth_TextBox.Text.ToString(),
                    program = "progra",
                    rank = "gegs",
                    services = "gegsg",
                    status = "geh",
                    yearsOfTeachingO = 1,
                    yearsOfTeachingS = 1
                };
                collection.Insert(entity);



            }
            catch (FormatException ex)
            {
                Console.WriteLine(ex);
            }
        }
        catch (MongoConnectionException ex)
        {
            Console.WriteLine(ex);
        }

        var query1 = collection.FindAllAs<facultyData>().SetFields(Fields.Include("facultyID", "term", "acadYear", "age", "program", "lastName", "firstName", "middleName", "dateOfBirth", "rank", "yearsOfTeachingS", "yearsOfTeachingO", "status", "services"));
        // List<facultyData> resultList = query.ToList<facultyData>();
        resultBinding = new ObservableCollection<facultyData>(query1);
        facultyDataGrid.ItemsSource = resultBinding;



回答2:


I think here:

<DataGridTextColumn x:Name="col_NumOrd" Binding="{Binding Path=NumOrd}"/>

You binding to NumOrd but here:

SelectDati_Viaggio.NumOrd= "abcabc";
OnPropertyChanged("SelectDati_Viaggio");

Notifing SelectDati_Viaggio, you need to notify NumOrd

SelectDati_Viaggio.NumOrd= "abcabc";
OnPropertyChanged("NumOrd");

than your control knows that property updated and refreshes

EDIT: edit your SelectDati_Viaggio class

private string numOrd;
public string NumOrd 
{ 
   get { return numOrd; } 
   set { numOrd = value; OnPropertyChanged("NumOrd"); }
}

Hope helps!



来源:https://stackoverflow.com/questions/28739678/datagrid-i-cant-refresh-field

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