4

基になる ViewModel クラスの列挙型にバインドできる WPF MVVM アプリケーションのメニューでチェックボックスを使用する例を見つけようとしています。私は簡単な例として持っています:

public class MyViewModel
{

   public MyViewModel() // constructor
   {
      MyChosenColor = Colors.Red;  // Pick red by default...
   }
   public enum Colors
   {
      Red,
      Green,
      Blue,   // this is just an example.  Could be many more values...
   }
   public Colors MyChosenColor {get; set;}
}

ユーザーがメニュー項目の「色」を選択し、赤、緑、青に赤がチェックされていることを確認できるようにするXAML(および必要に応じて最小限のコードバインド、コンバーターなど)が必要です(最初に)。Blue をチェックすると、MyChosenColor プロパティが Blue に設定され、チェックが Blue に変更されます。有望なリンクをいくつか見つけました: 相互に排他的なチェック可能なメニュー項目? RadioButtons を列挙型にバインドする方法は?

しかし、それらのどれもすべての問題 (相互に排他的なチェックボックス、ラジオ ボタンではなくチェックボックス) を処理しているようには見えず、多くの場合、多くのコード ビハインドが関係しています。私は Visual Studio 2012 を使用しているので、今までにより良い方法があるか、見落としている可能性がありますか?

列挙型にバインドされたメニュー内の相互排他的なチェックボックスのアイデアは、最も一般的なアイデアであると考えなければなりません。ありがとう。

4

3 に答える 3

4

レイチェルからのコメントのおかげで、私は以下の答えを提案します. これを行う必要がある人の助けになることを願っています。私は周りを検索しましたが、明示的に書き留められた例は見当たりませんでした。気にするのは単純すぎるかもしれません:)すべてをまとめて作業するのは少し面倒だと思ったので、ここに書き留めておきます。ありがとうレイチェル!

<Window x:Class="Demo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="clr-namespace:Demo"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>

</Window.Resources>
<DockPanel>
    <Menu DockPanel.Dock="Top">
        <MenuItem Header="Number Of Players"  ItemsSource="{Binding Path=MyCollection}">
            <MenuItem.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Header" Value="{Binding Title}" />
                    <Setter Property="IsCheckable" Value="True" />

                    <Setter Property="IsChecked" Value="{Binding IsChecked,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <Setter Property="Command" Value="{Binding DataContext.MyCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}}" />
                    <Setter Property="CommandParameter" Value="{Binding Player}" />
                </Style>
            </MenuItem.ItemContainerStyle>


        </MenuItem>

        </Menu>

    <Grid>

</Grid>
</DockPanel>

ここにViewModelコードがあります:

namespace Demo.ViewModel
{
public class MainViewModel : ViewModelBase
{

    public MainViewModel()
    {
       _myCollection = new ObservableCollection<NumberOfPlayersClass>();
        foreach (NumberOfPlayersEnum value in Enum.GetValues(typeof(NumberOfPlayersEnum)))
        {
            NumberOfPlayersClass myClass = new NumberOfPlayersClass();
            myClass.Player = value;
            myClass.IsChecked = value == NumberOfPlayersEnum.Two ? true : false; // default to using 2 players
            myClass.Title = Enum.GetName(typeof(NumberOfPlayersEnum), value);
            _myCollection.Add(myClass);
        }
    }
    private ICommand _myCommand;
    public ICommand MyCommand
    {
        get
        {
            if (_myCommand == null)
            {
                _myCommand = new RelayCommand(new Action<object>(ResolveCheckBoxes));

            }
            return _myCommand;
        }
    }



    ObservableCollection<NumberOfPlayersClass> _myCollection = new ObservableCollection<NumberOfPlayersClass>();
    public ObservableCollection<NumberOfPlayersClass> MyCollection
    {
        get
        {
           return _myCollection;
        }

    }
    public enum NumberOfPlayersEnum
    {
        One = 1,
        Two =2,
        Three =3,
    }
    public class NumberOfPlayersClass : ViewModelBase
    {
        public NumberOfPlayersClass()
        {
            IsChecked = false;
        }
        public NumberOfPlayersEnum Player { get; set; }
        private bool _isChecked = false;

        public bool IsChecked
        { get 
        { return _isChecked;
        }
            set
            {
                _isChecked = value;
                OnPropertyChanged("IsChecked");
            }

       }
        public string Title { get; set; }

    }

    private void ResolveCheckBoxes(object checkBoxNumber)
    {
        NumberOfPlayersEnum myEnum = (NumberOfPlayersEnum)checkBoxNumber;
        ObservableCollection<NumberOfPlayersClass> collection = MyCollection;
        NumberOfPlayersClass theClass = collection.First<NumberOfPlayersClass>(t => t.Player == myEnum);

            // ok, they want to check this one, let them and uncheck all else
            foreach (NumberOfPlayersClass iter in collection)
            {
                iter.IsChecked = false;
            }
            theClass.IsChecked = true;



    }
}
/// <summary>
/// A command whose sole purpose is to 
/// relay its functionality to other
/// objects by invoking delegates. The
/// default return value for the CanExecute
/// method is 'true'.
/// </summary>
public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    /// <summary>
    /// Creates a new command that can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    /// <summary>
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion // ICommand Members
}
}

/// <summary>
/// Base class for all ViewModel classes in the application.
/// It provides support for property change notifications 
/// and has a DisplayName property.  This class is abstract.
/// </summary>
public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
{
    #region Constructor

    protected ViewModelBase()
    {
    }

    #endregion // Constructor

    #region DisplayName

    /// <summary>
    /// Returns the user-friendly name of this object.
    /// Child classes can set this property to a new value,
    /// or override it to determine the value on-demand.
    /// </summary>
    public virtual string DisplayName { get; protected set; }

    #endregion // DisplayName

    #region Debugging Aides

    /// <summary>
    /// Warns the developer if this object does not have
    /// a public property with the specified name. This 
    /// method does not exist in a Release build.
    /// </summary>
    [Conditional("DEBUG")]
    [DebuggerStepThrough]
    public void VerifyPropertyName(string propertyName)
    {
        // Verify that the property name matches a real,  
        // public, instance property on this object.
        if (TypeDescriptor.GetProperties(this)[propertyName] == null)
        {
            string msg = "Invalid property name: " + propertyName;

            if (this.ThrowOnInvalidPropertyName)
                throw new Exception(msg);
            else
                Debug.Fail(msg);
        }
    }

    /// <summary>
    /// Returns whether an exception is thrown, or if a Debug.Fail() is used
    /// when an invalid property name is passed to the VerifyPropertyName method.
    /// The default value is false, but subclasses used by unit tests might 
    /// override this property's getter to return true.
    /// </summary>
    protected virtual bool ThrowOnInvalidPropertyName { get; private set; }

    #endregion // Debugging Aides

    #region INotifyPropertyChanged Members

    /// <summary>
    /// Raised when a property on this object has a new value.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raises this object's PropertyChanged event.
    /// </summary>
    /// <param name="propertyName">The property that has a new value.</param>
    protected virtual void OnPropertyChanged(string propertyName)
    {
        this.VerifyPropertyName(propertyName);

        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

    #endregion // INotifyPropertyChanged Members

    #region IDisposable Members

    /// <summary>
    /// Invoked when this object is being removed from the application
    /// and will be subject to garbage collection.
    /// </summary>
    public void Dispose()
    {
        this.OnDispose();
    }

    /// <summary>
    /// Child classes can override this method to perform 
    /// clean-up logic, such as removing event handlers.
    /// </summary>
    protected virtual void OnDispose()
    {
    }

#if DEBUG
    /// <summary>
    /// Useful for ensuring that ViewModel objects are properly garbage collected.
    /// </summary>
    ~ViewModelBase()
    {
        string msg = string.Format("{0} ({1}) ({2}) Finalized", this.GetType().Name,      this.DisplayName, this.GetHashCode());
        System.Diagnostics.Debug.WriteLine(msg);
    }
#endif

    #endregion // IDisposable Members
}

クラス RelayCommand および ViewModelBase に関する情報は、http: //msdn.microsoft.com/en-us/magazine/dd419663.aspxおよびhttp://rachel53461.wordpress.com/2011/05/08/simplemvvmexample/で入手できます 。

于 2013-04-16T23:22:44.337 に答える
0

RoutedUICommands、列挙型、および DataTriggers を使用するアプローチについては、「相互に排他的なチェック可能なメニュー項目?」に対する私の回答を参照してください。

于 2013-07-13T18:48:11.807 に答える