0

このコードは、VisualStudioMVVMテンプレートの委任コマンドがMenuItemおよびButtonと一緒に使用された場合に異なる動作をすることを示しています。

  • Buttonを使用すると、コマンドメソッドはViewModelで変更されたOnPropertyChanged値にアクセスできます。
  • ただし、MenuItemを使用する場合、コマンドメソッドは変更されたOnPropertyChanged値にアクセスできません。

なぜこれが当てはまるのか誰かが知っていますか?

MainView.xaml:

<Window x:Class="TestCommand82828.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:TestCommand82828.Commands"
    Title="Main Window" Height="400" Width="800">

    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Command="{Binding DoSomethingCommand}" Header="Do Something" />
            </MenuItem>
        </Menu>
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top">
            <Button Command="{Binding DoSomethingCommand}" Content="test"/>
            <TextBlock Text="{Binding Output}"/>
            <TextBox Text="{Binding TheInput}"/>
        </StackPanel>

    </DockPanel>
</Window>

MainViewModel.cs:

using System;
using System.Windows;
using System.Windows.Input;
using TestCommand82828.Commands;

namespace TestCommand82828.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        #region ViewModelProperty: TheInput
        private string _theInput;
        public string TheInput
        {
            get
            {
                return _theInput;
            }

            set
            {
                _theInput = value;
                OnPropertyChanged("TheInput");
            }
        }
        #endregion

        #region DelegateCommand: DoSomething
        private DelegateCommand doSomethingCommand;

        public ICommand DoSomethingCommand
        {
            get
            {
                if (doSomethingCommand == null)
                {
                    doSomethingCommand = new DelegateCommand(DoSomething, CanDoSomething);
                }
                return doSomethingCommand;
            }
        }

        private void DoSomething()
        {
            Output = "did something, the input was: " + _theInput;
        }

        private bool CanDoSomething()
        {
            return true;
        }
        #endregion            

        #region ViewModelProperty: Output
        private string _output;
        public string Output
        {
            get
            {
                return _output;
            }

            set
            {
                _output = value;
                OnPropertyChanged("Output");
            }
        }
        #endregion

    }
}
4

1 に答える 1

3

あなたが見ているのは、MenuItemsがTextBoxからフォーカスを奪わないためだと思います。デフォルトでは、TextBoxは、フォーカスが離れたときにのみ、変更をバインドされたソースにプッシュします。したがって、ボタンをクリックすると、フォーカスがボタンに移動し、TextBoxの値が_theInputに書き戻されます。ただし、MenuItemをクリックすると、フォーカスはTextBoxに残り、値は書き込まれません。

TextBox宣言を次のように変更してみてください。

<TextBox Text="{Binding TheInput,UpdateSourceTrigger=PropertyChanged}"/>

DelegateCommand<t>または、パラメータを受け取ることができるに切り替えて、TextBoxのテキストを渡すこともできます。

<MenuItem Command="{Binding DoSomethingCommand}" 
          CommandParameter="{Binding Text,ElementName=inputTextBox}" />
...
<TextBox x:Name="inputTextBox" Text="{Binding TheInput}" />
于 2009-06-05T09:55:44.587 に答える