I have the following XAML:
<DataGrid ItemsSource="{Binding Path=FilteredPatients}" SelectedItem="{Binding Path=SelectedPatient}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"
Binding="{Binding Path=FormattedName}" />
<DataGridTextColumn Header="Date of Birth"
Binding="{Binding Path=BirthDate} />
<DataGridTextColumn Header="Gender"
Binding="{Binding Path=Gender} />
<DataGridTextColumn Header="Id"
Binding="{Binding Path=Id}" />
</DataGrid.Columns>
</DataGrid>
Resharper determines that FilteredPatients and SelectedPatient are ok based on the DataContext of the parent control. However, FilteredPatients is an ICollectionView and thus Resharper cannot figure out that it contains instances of Patient, which has the properties specified in the DataGrid column bindings.
Everything works fine at runtime, so how do I tell Resharper the type of item contained by FilteredPatients?
The simplest solution is to disable the Resharper error for the columns since it is incorrect about this issue:
<!-- ReSharper disable Xaml.BindingWithContextNotResolved -->
<DataGridTextColumn />
<DataGridTextColumn />
<!-- ReSharper restore Xaml.BindingWithContextNotResolved -->
A real solution is to use a DataGridTemplateColumn instead of DataGridTextColumn. This lets you identify the DataType in the DataTemplate for each of the columns, but requires more xaml:
<DataGrid ItemsSource="{Binding Path=FilteredPatients}" SelectedItem="{Binding Path=SelectedPatient}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="namespace:Patient">
<TextBlock Text={Binding FormattedName} />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With