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



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

我有一个带有图像和上下文菜单的按钮。每次我单击上下文菜单项时,我都想更改按钮的图像。使用下面的代码,我可以在右键单击时显示上下文菜单项。但不知道如何继续。有人能给我指路吗?奇怪的是,我试着使用命令,但命令从未被调用。

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

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

最新更新