Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell Resharper the type contained by an ICollectionView?

Tags:

c#

resharper

wpf

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?

like image 290
17 of 26 Avatar asked Sep 25 '22 12:09

17 of 26


1 Answers

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>
like image 184
JNP Avatar answered Sep 28 '22 05:09

JNP