ContextMenu的图标不会显示出来

时间:2014-05-09 14:02:10

标签: wpf image mvvm

答案my last question我可以解决我的ContextMenu问题。 现在我想在MenuItem上显示一个小图标。并且不幸的是。

这里的代码不起作用(我只设置按钮的图像!树视图应该类似):

MainWindow.xaml:

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

    <Grid >
        <StackPanel>
            <!--To show that it works in principal:-->
            <TextBlock Text="{Binding MyText}" />
            <Image Source="{Binding MyImage}" Width="25" Height="25"/>

            <Button Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}" Content="Click me">
                <Button.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="{Binding PlacementTarget.Tag.MyText,
                            RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}">
                            <MenuItem.Icon>
                                <Image Source="{Binding PlacementTarget.Tag.MyImage,
                                    RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" />
                            </MenuItem.Icon>
                        </MenuItem>
                    </ContextMenu>
                </Button.ContextMenu>
            </Button>

            <TreeView ItemsSource="{Binding MyList}" >
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <TextBlock Text="{Binding Name}" Tag="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}">
                            <TextBlock.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="{Binding Path=PlacementTarget.Tag.MyText, 
                                        RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" />
                                </ContextMenu>
                            </TextBlock.ContextMenu>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>         
            </TreeView>

        </StackPanel>        
    </Grid>
</Window>

MainWindowVM.cs:

public class MainWindowVM
{
    private ImageSource _myImage;
    public string MyText { get; set; }
    public ObservableCollection<TreeElement> MyList { get; set; }

    public ImageSource MyImage { get { return _myImage; } }

    public MainWindowVM()
    {
        MyText = "This is my Text!";

        _myImage = new BitmapImage(new Uri("/ContextMenu;component/Resources/flash.png", UriKind.Relative));

        MyList = new ObservableCollection<TreeElement>();
        MyList.Add(new TreeElement("String 1"));
        MyList.Add(new TreeElement("String 2"));
    }
}

public class TreeElement
{
    public string Name { get; set; }
    public TreeElement(string Name)
    {
        this.Name = Name;
    }
}

感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

这段代码可能会导致绑定失败

RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}

首先,任何元素都会自动继承其可视父级的DataContext,因此您的MenuItem已经获得了正确的DataContext,允许它在没有RelativeSource的情况下进行绑定。

其次,这是更难的部分,WPF中弹出的任何内容(上下文菜单,工具提示和Xceed工具包中的几个可扩展控件)实际上都不是可视树的一部分,不会起作用适当地使用RelativeSource或ElementName绑定。但是,大多数事情似乎都会使用正确的DataContext自动填充他们的第一个孩子,因此我经常使用ContentControl来绑定到根级别。

希望这有帮助