我是靠谱客的博主 壮观西牛,最近开发中收集的这篇文章主要介绍mvvm绑定checkbox wpf_WPF(MVVM)菜单中的互斥(和可绑定)复选框,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

感谢Rachel的评论,我提出以下答案.我希望它可以帮助那些需要这样做的人.我四处搜索,并没有看到明确写下的例子.也许这太麻烦了太烦了:)我发现把所有东西拉到一起并且工作有些痛苦,所以我在这里写下来.再次感谢瑞秋!

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

这是ViewModel代码:

namespace Demo.ViewModel

{

public class MainViewModel : ViewModelBase

{

public MainViewModel()

{

_myCollection = new ObservableCollection();

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(ResolveCheckBoxes));

}

return _myCommand;

}

}

ObservableCollection _myCollection = new ObservableCollection();

public ObservableCollection 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 collection = MyCollection;

NumberOfPlayersClass theClass = collection.First(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;

}

}

///

/// 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'.

///

public class RelayCommand : ICommand

{

#region Fields

readonly Action _execute;

readonly Predicate _canExecute;

#endregion // Fields

#region Constructors

///

/// Creates a new command that can always execute.

///

/// The execution logic.

public RelayCommand(Action execute)

: this(execute, null)

{

}

///

/// Creates a new command.

///

/// The execution logic.

/// The execution status logic.

public RelayCommand(Action execute, Predicate 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

}

}

///

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

///

public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable

{

#region Constructor

protected ViewModelBase()

{

}

#endregion // Constructor

#region DisplayName

///

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

///

public virtual string DisplayName { get; protected set; }

#endregion // DisplayName

#region Debugging Aides

///

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

///

[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);

}

}

///

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

///

protected virtual bool ThrowOnInvalidPropertyName { get; private set; }

#endregion // Debugging Aides

#region INotifyPropertyChanged Members

///

/// Raised when a property on this object has a new value.

///

public event PropertyChangedEventHandler PropertyChanged;

///

/// Raises this object's PropertyChanged event.

///

/// The property that has a new value.

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

///

/// Invoked when this object is being removed from the application

/// and will be subject to garbage collection.

///

public void Dispose()

{

this.OnDispose();

}

///

/// Child classes can override this method to perform

/// clean-up logic, such as removing event handlers.

///

protected virtual void OnDispose()

{

}

#if DEBUG

///

/// Useful for ensuring that ViewModel objects are properly garbage collected.

///

~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

}

最后

以上就是壮观西牛为你收集整理的mvvm绑定checkbox wpf_WPF(MVVM)菜单中的互斥(和可绑定)复选框的全部内容,希望文章能够帮你解决mvvm绑定checkbox wpf_WPF(MVVM)菜单中的互斥(和可绑定)复选框所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(101)

评论列表共有 0 条评论

立即
投稿
返回
顶部