问题绑定WPF的ContextMenu到一个ObservableCollection用转换器 [英] Problems binding a WPF ContextMenu to an ObservableCollection with a Converter

查看:102
本文介绍了问题绑定WPF的ContextMenu到一个ObservableCollection用转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有更好的方式来做到这一点,但这里是我在哪里现在。我想一个文本菜单绑定到一些复杂类型的一个ObservableCollection。在复杂类型的属性将决定被显示在菜单项的图标。我知道要做到这一点的唯一方法是创建一个ComplexTypeToMenuItem转换器和准与绑定。然而,当我的转换器添加到集合时改变了不再结合的上下文菜单的更新。如果我删除转换器和依靠ComplexType.ToString(),那么菜单项更新就好了。但是,也有在这种情况下,没有图标

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:

初在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>

和现在的代码

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!

推荐答案

的ObservableCollection 列表,这并不再提供更新,因此这个问题。一般来说,你要避免的ItemsSource 绑定转换器。

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.

这篇关于问题绑定WPF的ContextMenu到一个ObservableCollection用转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆