0

I have a icon(rectangle) in my XAML which has a contextmenu bound to it. What I want to achive is for the icon and the contextmenu to bind some values togheter in a string, which is later going to be used in another method.

To give a more vivid example, I have four icons, the represent four physical object. In the contextmenu I have a connectionstring to all four objects. The information I need from the action of pressing the icons and the connectionstring via the contextmenu builds the final connectionstring, ie. from object to object. To make it a little bit more complex, both the connectionstrings and the icons are dynamically handed via an API.

The problem I'm having is that I can't find a good way bind my icon to the value it is representing, when I'm using the contextmenu, since the values of the contextmenu are bound to one list and the other value is bound to a string. I wouldn't like to have a x:Name approach that is static, since it wouldn't make an oppertunity to also later create the icons dynamically, this approach I did try, as you can see in the code and was a success, but as I said, it would be prefeered to have a more dynamic approach.

<Rectangle Tag="{Binding TrackID}" x:Name="Test" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="23" Margin="122,117,0,0" Stroke="Black" VerticalAlignment="Top" Width="28">
            <Rectangle.ContextMenu>
                <ContextMenu ItemsSource="{Binding tracksMenuList1}" ContextMenuClosing="ContextMenu_ContextMenuClosing" MenuItem.Click="ContextMenu_Click">
                <ContextMenu.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="Header" Value="{Binding Tracks}"/>
                        <Setter Property="Uid" Value="{Binding id}"/>
                    </Style>
                </ContextMenu.ItemContainerStyle>
                </ContextMenu>
            </Rectangle.ContextMenu>
        </Rectangle>
Calleman
  • 25
  • 6
  • Why can't this be handled in your view model? If it's dynamic why not just change the value within the view model? – Ben Jul 04 '20 at 23:36
  • I'm not really sure I follow. At the current time the icons are not created dynamically. The contextmenu only gets the values from the viewmodel, nothing is passed back. I should add that the "Tag=..." doesn't work. The contextmenu on ContextMenu_Click doesn't take any other variables except for those belonging to the contextmenu itself. – Calleman Jul 04 '20 at 23:45
  • I was suggesting something like this: https://stackoverflow.com/questions/15566824/contextmenu-in-mvvm, though, now not sure if that's what you're asking for. – Ben Jul 04 '20 at 23:56

0 Answers0