Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

XML
<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.
XML
<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".

XML
<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>
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jun-14 19:48pm    
The attempts to do everything in XAML are just fruitless and counter-productive. Isn't it obvious that it's possible to formulate the problem which cannot be solved in pure XAML, and it can be formally proven? If so, why ramming some particular problems, which are in that sense artificial? Isn't it obvious that your problem is not a problem at all is solved in code? Why programming in XAML for the sake of XAML? Your goals should be behavior of your application and its maintainability...
—SA
cigwork 29-Jun-14 3:18am    
I know I can solve this problem in code, I've already done so. Artificial problem? Yes. Where does it say questions on this forum have to be for, "real world problems"? This is an experiment/tutorial exercise to see whether or not XAML lives up to the hype and because it looked like a fun thing to try.
Sergey Alexandrovich Kryukov 29-Jun-14 21:36pm    
Very good. My advice would be not to take this problem related to XAML purism seriously, but I certainly can understand your motivation. Good luck.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900