Click here to Skip to main content
14,830,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i have a problem with the databinding of a datagrid in WPF.

my model:
C#
public class CheckLogModel : ObservableObject
{
    public int Id { get; set; }
    public DateTime TimeStamp { get; set; }
    public decimal MandantNr { get; set; }
    public decimal PrintJobNr { get; set; }
    public decimal HgoNr { get; set; }
    public decimal GoNr { get; set; }
}

MainviewModel:
C#
private ObservableCollection<CheckLogModel> _checkLogEntries = new ObservableCollection<CheckLogModel>();

public ObservableCollection<CheckLogModel> CheckLogEntries {
    get {
        return _checkLogEntries;
    }
    set {
        if (value == _checkLogEntries) return;
        _checkLogEntries = value;
        RaisePropertyChangedEvent("CheckLogEntriesChanged");
    }
}
C#
private void SearchWqButtonClick(object sender) {
        var searchModel = new SearchCheckLogModel {
            MandantNr = _selectedMandant.Nummer,
            GoNr = Converter.ConvertToDecimal(_goNr),
            HgoNr = Converter.ConvertToDecimal(_hgoNr),
            PrintJobNr = Converter.ConvertToDecimal(_printjobNr),
            Id = Converter.ConvertToInt(_id)
        };

        if (searchModel.IsAllCriteriasEmpty()) {
            //Fehler ausgeben
            return;
        }
        //Search
        CheckLogEntries = CheckLogRepository.GetCheckLogEntities(searchModel);
    }
}

Main Window XAML:
DataContext="{Binding Main, Source={StaticResource Locator}}" 

XML
<DataGrid ItemsSource="{Binding CheckLogEntries}"
          Grid.Column="1" 
          Grid.Row="0"
          AutoGenerateColumns="true"
          >
</DataGrid>

App.xaml:
XML
<application.resources>
    <resourcedictionary>
      <vm:viewmodellocator x:key="Locator" 
="" d:isdatasource="True" xmlns:vm="clr-namespace:NetigateLogViewer.ViewModel">


What I have tried:

When executing the programm I get a correct resultset of "CheckLogEntries" from the static method "CheckLogRepository.GetCheckLogEntities" when i execute the method "SearchWqButtonClick" plus a correct execution of the property setter so that the property should be set.

But the rows are not displayed in the datagrid of the main window.
I guess there is sth wrong with the binding, but have no idea what it is...

Any help is appreciated?
Posted
Updated 30-Sep-20 22:43pm
v2

1 solution

Quote:
C#
RaisePropertyChangedEvent("CheckLogEntriesChanged");
That looks like a good place to start. Your property is called CheckLogEntries, but you're raising the "property changed" event for a property called CheckLogEntriesChanged.

Change that line to use the correct property name. You can use nameof[^] to ensure you get the correct name:
C#
RaisePropertyChangedEvent(nameof(CheckLogEntries));
   
Comments
C.Rath 1-Oct-20 5:56am
   
Thx works fine

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900