5

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?

17 of 26
  • 27,121
  • 13
  • 66
  • 85
  • Does this answer your question? [Wpf ICollectionView Binding item cannot resolve property of type object](https://stackoverflow.com/questions/20254690/wpf-icollectionview-binding-item-cannot-resolve-property-of-type-object) – Maxence Nov 16 '19 at 16:55

3 Answers3

3

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>
JNP
  • 131
  • 2
  • 5
  • I don't want to disable the warning, which is the point of my question. The problem with the second solution is that it makes the XAML much longer. I was hoping for a way to simply tell Resharper what the contained objects are in one line. – 17 of 26 Feb 01 '16 at 18:27
  • The other way you ordinarily would be able to do that is to set the d:DataContext individually on the DataGridTextColumns (I assume you have already done this for the whole view), however as far as I can tell that doesn't work in this case. I tried that out when looking into this and Resharper still doesn't like that. But you could play with that and perhaps there is a way to get that to work. – JNP Feb 01 '16 at 18:42
1

JetBrains has acknowledged that this is a bug and have slated it to be fixed in the next release.

17 of 26
  • 27,121
  • 13
  • 66
  • 85
  • 1
    It still isn't fixed 3 years later. I think JNPs answer is the only real option here – Bijington Sep 17 '19 at 07:55
  • Yeah, that's what I've been doing while waiting for a fix :( – 17 of 26 Sep 17 '19 at 13:58
  • I did also stumble across this approach of implementing a generic `ICollectionView` here: https://benoitpatra.com/2014/10/12/a-generic-version-of-icollectionview-used-in-a-mvvm-searchable-list/ however it caused issues in our scenario when we turn grouping on/off dynamically – Bijington Sep 17 '19 at 14:39
0

Since this is design time and Resharper one way to let it know is to set the DesignInstance at the top like so:

d:DataContext="{d:DesignInstance {x:Type viewModels:MyViewModel}}"

This will not affect production but will let Resharper reflect on the values in your ViewModel.

Kelly
  • 6,992
  • 12
  • 59
  • 76
  • I've done that, which is why the bindings on the DataGrid itself are ok (as I mentioned in the question). The problem is that ICollectionView exposes an IEnumerable with no type, so Resharper doesn't know the type contained by FilteredPatients. – 17 of 26 Feb 02 '16 at 13:56