Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:55:48+00:00 2026-05-28T06:55:48+00:00

Perhaps there is a better way to do this, but here’s where I’m at

  • 0

Perhaps there is a better way to do this, but here’s where I’m at right now. I would like to bind a ContextMenu to an ObservableCollection of some complex type. A property on the complex type will determine the icon that is displayed in the menu item. The only way I knew to do this was to create a ComplexTypeToMenuItem converter and associate that with the binding. However, as soon as I add the converter to the binding the context menu no longer updates when the collection has changed. If I remove the converter and rely on ComplexType.ToString() then the menu items update just fine. However, there are no icons in this case.

This can easily be reproduced with the following:

First the XAML

<Window x:Class="GoddamnContextMenuPosition.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:GoddamnContextMenuPosition" 
        Title="MainWindow" Height="350" Width="525">

  <Window.Resources>
    <local:ComplexTypeToMenuItemConverter x:Key="ComplexTypeToMenuItemConverter" />
  </Window.Resources>

    <Grid>
        <TextBox Grid.Row="0" Text="Hooray">
            <TextBox.ContextMenu>
                <ContextMenu ItemsSource="{Binding Objects, Source={x:Static local:Settings.Instance}, Converter={StaticResource ComplexTypeToMenuItemConverter}}">
                </ContextMenu>
            </TextBox.ContextMenu>
        </TextBox>

        <Button Grid.Row="1" Content="Add" Height="25" Width="50" Click="Button_Click"></Button>
    </Grid>
</Window>

And now the code

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Settings.Instance.Objects.Add(new ComplexType()
        {
            Text = "NEW", Type = 4
        });
    }
}

public class ComplexType
{
    public ComplexType()
    {
        this.TimeStamp = DateTime.Now;
    }

    public string Text
    {
        get;
        set;
    }

    public int Type
    {
        get;
        set;
    }

    public DateTime TimeStamp
    {
        get;
        set;
    }
}

public class ComplexTypeToMenuItemConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var values = value as IEnumerable<ComplexType>;

        if (values == null)
        {
            return new List<MenuItem>
            {
                new MenuItem
                {
                    Header = "Unknown Value"
                }
            };
        }

        IList<MenuItem> items = new List<MenuItem>();

        foreach (ComplexType obj in values)
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri("1.png", UriKind.Relative);
            bitmap.EndInit();

            Image image = new Image();
            image.Width = 16;
            image.Source = bitmap;

            MenuItem menuItem = new MenuItem();
            menuItem.Header = string.Format("{0} - {1}", obj.Text, obj.TimeStamp.ToShortTimeString());
            menuItem.Icon = image;

            items.Add(menuItem);
        }

        return items;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public class Settings
{
    private static readonly Settings settings = new Settings();

    private Settings()
    {
        this.Objects = new ObservableCollection<ComplexType>
        {
            new ComplexType
            {
                Text = "Item #1",
                Type = 1
            }
        };
    }

    public static Settings Instance
    {
        get
        {
            return settings;
        }
    }

    public ObservableCollection<ComplexType> Objects
    {
        get;
        private set;
    }
}

Any help will be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:55:48+00:00Added an answer on May 28, 2026 at 6:55 am

    You turn the ObservableCollection into a List, which does no longer provide updates, hence the problem. In general you want to avoid converters in ItemsSource bindings.

    Do not use converters to do this sort of thing, use Data Templating.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Perhaps there's a better way to set this up so I'm open to suggestions.
Is there any way to simplify this method? Perhaps a way to && both
This is one of those there's gotta be a better way questions. Let me
Does anyone have a good VBA way (or perhaps there is an MS Word
Is there an equivalent to 'intellisense' for Python? Perhaps i shouldn't admit it but
Perhaps this is an effect of installing the new 4.5 beta, but my simulator
This seems like a noob question, but the simple answer is eluding me. I
Maybe I'm looking at this problem all wrong, but here goes. We have a
Are there any editors that can edit multi-gigabyte text files, perhaps by only loading
is there anyway to save the following flash video locally, perhaps by streaming it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.