选择上下文菜单项时更改按钮图像

时间:2013-08-10 21:37:13

标签: wpf mvvm

有人可以告诉我如何在单击上下文菜单项时更改按钮图像吗?

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

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

1 个答案:

答案 0 :(得分:0)

首先,我认为您需要将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);
    }
}