开发者

Command not working for menu item

I need help to figure out why my command doesn't work on menu item. I've been googling the solutions for this, and found few too in here. But probably because of my knowledge (beginner WPF), I still couldn't solve it. Any help is appreciated!

It works with button, but not with the menu item.

XAML:

    <Window x:Class="WPFBeginner.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="349" Width="259">
    <Grid>
        <Grid.RowDefinitions开发者_运维知识库 />
        <Grid.ColumnDefinitions />
        <Menu Height="22" HorizontalAlignment="Left" Name="menu1" VerticalAlignment="Top" Width="237" Margin="0,1,0,0">
            <MenuItem Header="_File" >
                <MenuItem Header="Save As" Command="{Binding SaveCommand}"/>
                <Separator />
                <MenuItem Command="Close" />
            </MenuItem>
            <MenuItem Header="_Edit">
                <MenuItem Command="Undo" />
                <Separator />
                <MenuItem Command="Cut" />
                <MenuItem Command="Copy" />
                <MenuItem Command="Paste" />
                <Separator />
                <MenuItem Command="SelectAll" />
            </MenuItem>
        </Menu>
        <TextBox Height="217" HorizontalAlignment="Left" Margin="0,21,0,0" Name="txtBox1" VerticalAlignment="Top" Width="238" 
                 Text="{Binding Note.Data}" />
        <!--button works fine-->
        <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="12,244,0,0" Name="button1" VerticalAlignment="Top" Width="75" 
                Command="{Binding SaveCommand}"/>
    </Grid>
</Window>

Here's the code for the ViewModel.

    class NoteViewModel : INotifyPropertyChanged
{
    public ICommand SaveCommand { get; set; }

    public NoteViewModel()
    {
        SaveCommand = new RelayCommand(Save);
        Note = new NoteModel();
    }

    private NoteModel note;
    public NoteModel Note
    {
        get { return note; }
        set
        {
            if (note != value)
            {
                note = value;
                RaisedPropertyChanged("Note");
            }
        }
    }

    private void Save()
    {
        SaveFileDialog file = new SaveFileDialog();

        if ((bool)file.ShowDialog())
        {
            File.WriteAllText(file.FileName, Note.Data, Encoding.UTF8);
        }
    }

    #region ...INPC
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisedPropertyChanged(string p)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(p));
    }
    #endregion
}

I debug it, it turned out that the command (SaveCommand --> Save()) is executed, but the value of Note.Data is null. It's something if I use button instead.

EDIT: Extra information: I use RelayCommand from MVVMLight.


What's likely happening is that the TextBox still has focus when you select your menu item. By default, bindings in WPF update when the control loses focus (so that updates don't take place constantly, as they do when PropertyChange is the update type). When you use the button, the TextBox loses focus because the button gets it.

You can test this out by putting another control (any kind) on the window and clicking on it before selecting your menu item.

If this addresses the issue, then the simplest fix is to change the binding update type to PropertyChange (this can be done in the binding options area of the designer).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜