感谢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)菜单中的互斥(和可绑定)复选框相关推荐

  1. 在 jquery repeater 中添加设置日期,下拉,复选框等控件

    JQueryElement 更新到了 3.5.1, 今天给大家主要讲下如何在 Repeater 的模板中添加设置一些控件. 由于精力有限, 不能在多个博客中保证文章的同步, 可在如下地址查看最新内容, ...

  2. 在DataGrid中选择,确认,删除多行复选框列表

    在DataGrid中选择,确认,删除多行复选框列表 Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid ...

  3. Jquery第二章常用方法,一二级菜单淡入淡出,event事件,复选框的全选反选第一节

    hide() show() 通过滑动效果 slideToggle() 淡入淡出 fadeIn() fadeout() 向上 向下拉动 slideup() slidedown() 源码: <!DO ...

  4. ExtJs中定制日历控件——带复选框

    效果图: 代码: /*********************日历组件部分**************************** begin */ var dateArray = new Array ...

  5. react 全选反选_js中怎么将createElement出来的复选框实现全选,全不选,反选效果?...

    const checkBoxs = ["篮球","足球","羽毛球","乒乓球"]; const creactCheck ...

  6. 如何使用AngularJS绑定到复选框值列表?

    本文翻译自:How do I bind to list of checkbox values with AngularJS? I have a few checkboxes: 我有几个复选框: < ...

  7. Vue.js 极简小例:表单 (输入框 input、文本域 textarea、单选框 radio、下拉菜单 selected、复选框 checkbox)

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 代码: <template> <div > <p>------- ...

  8. CSS3重新定义input中呆若木鸡的默认复选框CheckBox和单选框Radio样式

    <!DOCTYPE html> <html> <head><meta charset="UTF-8"><style>/* ...

  9. php中得到复选框的数据的代码,表单复选框向PHP传输数据的代码

    表单复选框向PHP传输数据的代码 表单复选框就是checkbox 1.checkbox的应用 复制代码 代码如下: 2.由于我传输的是在php循环中产生的数组,因此value也要设成变量: for($ ...

  10. layui复选框怎么取值_layui获取多选框中的值方法

    layui获取多选框中的值方法 HTML: title="=$value;?>"> js: $("input:checkbox[name='standard' ...

最新文章

  1. 微信小程序把繁琐的判断用Js简单的解决
  2. 算了一挂,也不知准不准
  3. 实验六 数组 (2)
  4. 22.循环控制.rs
  5. P1160-队列安排【链表】
  6. vscode-textlive-paper学习记录
  7. linux工具-journalctl查询日志
  8. Android OpenGL 使用
  9. mysql自动去重_关于mysql自联去重的一些记录
  10. Linux用户和组账户管理
  11. 如何获取iphone的UUID
  12. Hexo-next主题优化篇
  13. maya批量文件修改插件 v1.0 下载及教程
  14. 华为vlan间路由:利用路由器实现不同vlan间的通信
  15. java代理模式学习笔记
  16. 练习2-1 Programming in C is fun
  17. 迅为iTOP-i.MX6ULL开发板I2C驱动程序实现 I2C通信
  18. Python3中StringIO
  19. python常用英文单词怎么写_python常用150个英文单词
  20. dialer(dialer接口是什么意思)

热门文章

  1. 【算法】排序_选择排序及其优化
  2. tc自动服务器,使用linux下的TC进行服务器流量控制
  3. HTML关联两个标签事件,javascript – 交换2个html元素并保留事件侦听器
  4. php添加开机启动脚本_php-fpm开机自动启动Shell脚本
  5. 浅谈C语言中的强符号、弱符号、强引用和弱引用【转】
  6. 一个不简洁的约瑟夫环解法
  7. mysql 查询时间戳(TIMESTAMP)转成常用可读时间格式
  8. 如何在代码里打开Android手机通知状态栏
  9. linux系统编程之进程(二):进程生命周期与PCB(进程控制块)
  10. 阿里云开发者大赛记事