Trying to set up the binding for a menu item's icon so that I can vary it according to the number (and state) of items selected.
I've tried a number of variations for the Image Source binding including the three shown below as well as a number of others I can't be bothered to repeat here.
Nothing seems to work. Binding is not established, all I get are variations on a theme of, "
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ListView', AncestorLevel='1''. BindingExpression:Path=SelectedItems; DataItem=null; target element is 'Image' (Name='startIcon'); target property is 'Source' (type 'ImageSource')
" with varying error codes (mainly 4).
It would seem I'm missing something obvious, but I can't see what it is.
<ListView x:Name="grdTasks"
ItemContainerStyle="{StaticResource SimpleListBoxItem}"
DockPanel.Dock="Top"
HorizontalAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch" Margin="2">
<ListView.ContextMenu>
<ContextMenu x:Name="ItemsMenu"
DataContext="{Binding Path=PlacementTarget.DataContext,
RelativeSource={RelativeSource Self}}" >
<MenuItem x:Name="start"
Header="Start"
Command="{Binding Path=StartCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu},
Path=PlacementTarget.SelectedItems}">
<MenuItem.Icon>
<Image x:Name="startIcon" Height="16" Width="16"
Source="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu, AncestorLevel=2},
Path=PlacementTarget.SelectedItems,
Converter={StaticResource GroupImageConverter}}">
</Image>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</ListView.ContextMenu>
</ListView>
...as well as targetting a named element.
<MenuItem.Icon>
<Image x:Name="startIcon" Height="16" Width="16"
Source="{Binding ElementName=grdTasks,
Path=SelectedItems,
Converter={StaticResource GroupImageConverter}}">
</Image>
</MenuItem.Icon>
...and this. Although I'd half expect this not to work because (according to our favourite search tool), "context menu is not part of the visual tree".
<MenuItem.Icon>
<Image x:Name="startIcon" Height="16" Width="16"
Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}},
Path=SelectedItems,
Converter={StaticResource GroupImageConverter}}">
</Image>
</MenuItem.Icon>