【问题标题】:Change button image when context menu item is selected选择上下文菜单项时更改按钮图像
【发布时间】:2013-08-13 01:43:12
【问题描述】:

谁能告诉我如何在单击上下文菜单项时更改按钮图像?

我有一个带有图像和上下文菜单的按钮。每次单击上下文菜单项时,我都想更改按钮的图像。 使用以下代码,我可以在右键单击时显示上下文菜单项。但不知道如何进一步进行。 谁能指导我? 我尝试使用命令奇怪地命令从未被调用。

  <Button Background="Gray" Name="statusBtn" VerticalAlignment="Top" HorizontalAlignment="Right" FontWeight="Bold" Foreground="Red">
            <DockPanel >
                <Image DockPanel.Dock="Top" Stretch="Fill" Source="{Binding ToEnum, Converter={StaticResource EnumToImgConverter}}"  Height="37" Width="72" />
                <TextBlock HorizontalAlignment="Center" Margin="0,23,1,2">Test</TextBlock>
            </DockPanel>
            <Button.ContextMenu>
                <ContextMenu Name="ContextMenuName" ItemsSource="{Binding Path=Popuplistitems}">
                    <ContextMenu.ItemTemplate >
                        <DataTemplate DataType="MenuItem">
                            <MenuItem Header="{Binding Message}" Command="{Binding popupListCommand}">
                                <MenuItem.ItemContainerStyle >
                                            <Style TargetType="MenuItem">
                                                <Setter Property="IsCheckable" Value="True"/>
                                                <Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
                                            </Style>
                                 </MenuItem.ItemContainerStyle>
                             </MenuItem>
                        </DataTemplate>
                    </ContextMenu.ItemTemplate>
                </ContextMenu>
            </Button.ContextMenu>
        </Button>

【问题讨论】:

  • 您说“使用以下代码”。代码在哪里?
  • 对不起。我用代码更新了我的帖子
  • 哦,抱歉没有读到您正在使用 wpf。也许这会有所帮助social.msdn.microsoft.com/Forums/vstudio/en-US/…
  • 感谢您的快速回复。但是如何获得选定的菜单项?我有 10 个菜单项,它们绑定在后面的代码中,我需要为每个选定的菜单项显示不同的图像。请耐心等待,因为我是 wpf\mvvm 的新手

标签: wpf mvvm


【解决方案1】:

首先,我认为您需要将 MenuItem.ItemContainerStyle 更改为仅 MenuItem.Style - 我相信 ItemContainerStyle 是用于您可以在需要嵌套上下文菜单时放入 MenuItem 的子菜单项。

我能够使用以下代码更改按钮的内容。我使用了文本内容,但应该很容易将其换成图像内容:

xaml:

<Window x:Class="ChangeButtonContent.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>
    <Button Height="30" Width="200">
        <Button.Content>
        <DockPanel >
            <TextBlock Text="{Binding ChangingText, Mode=TwoWay}" />
        </DockPanel>
        </Button.Content>
        <Button.ContextMenu>
            <ContextMenu Name="ContextMenuName">
                <MenuItem Command="{Binding ChangeTextCommand}" Header="ChangeText"/>
            </ContextMenu>
        </Button.ContextMenu>
    </Button>
</Grid>

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        ChangingText = "Initial Text";

        ChangeTextCommand = new MyCommand((notUsed) =>
        {
            ChangingText = "Text After Context Menu Click";
        });

    }

    private string _changingText;
    public string ChangingText
    {
        get
        {
            return _changingText;
        }
        set
        {
            _changingText = value;
            OnPropertyChanged("ChangingText");
        }
    }

    public MyCommand ChangeTextCommand
    {
        get;
        private set;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        var propertyChanged = PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class MyCommand : ICommand
{
    private Action<object> _action;
    public MyCommand(Action<object> action)
    {
        _action = action;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _action(parameter);
    }
}

【讨论】:

  • 感谢您的解决方案。我尝试了 xaml 方法。没有运气:(
  • 如果我尝试硬编码 menuitem 标头,则会调用命令,如下所示:
  • 感谢您的解决方案。我能够让这段代码与图像和硬编码标题一起使用。如果我尝试如下绑定标题,则会出错: 但我收到错误“项目集合必须是在使用 ItemsSource 之前为空。
  • 我将它绑定到 List obj.
  • 您收到该错误是因为您试图通过指定 ItemsSource 并显式添加 MenuItem 子项来将项目添加到上下文菜单中。这些是相互排斥的选项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多