Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper not happy about XAML

This piece of XAML (in the definition of a ListView)

    <ListView 
        x:Name="ListViewEpisodes" 
        Grid.Row="1" 
        Grid.Column="2" 
        ItemsSource="{Binding Episodes}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Episode Name" Width="Auto" DisplayMemberBinding="{Binding FileName}"  />
            </GridView>
        </ListView.View>
        <ListView.ContextMenu>
            <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Self}}">
                <MenuItem 
                    Header="Delete episode" 
                    Command="w:MainWindow.DeleteEpisode"

                    <!--  PlacementTarget.SelectedItem not OK according to Resharper -->

                    CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"/>
            </ContextMenu>
        </ListView.ContextMenu>
    </ListView>

Resharper warning:

Can not resolve property 'SelectedItem' in data context of type 'System.Windows.UIElement'        

Now, the XAML does work, when I right click an item, and execute the ContextMenuItem it does fill the parameter with the content of the Episode.

Why is Resharper not happy about this?

like image 401
bas Avatar asked Oct 14 '25 11:10

bas


1 Answers

This is a pretty easy to explain. PlacementTarget is of type UIElement that doesn't have Tag member. ReSharper tries to find member with Tag name but it fails.

At compile time ReSharper is right. ReSharper doesn't know what exactly type would be in run time.

Maybe you need to rewrite your binding expression to:

<ContextMenu DataContext="{Binding Tag, RelativeSource={RelativeSource Self}}" />

It's difficult to say precisely, because it isn't obvious what are you trying to achieve with this binding expression.

like image 141
Vladimir Almaev Avatar answered Oct 16 '25 23:10

Vladimir Almaev