0

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

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

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

1 回答 1

0

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

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

xml:

<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);
    }
}
于 2013-08-11T00:35:41.933 回答