我有以下用于单选按钮绑定(bind)的类
public class RadioButtonSwitch : ViewModelBase
{
IDictionary<string, bool> _options;
public RadioButtonSwitch(IDictionary<string, bool> options)
{
this._options = options;
}
public bool this[string a]
{
get
{
return _options[a];
}
set
{
if (value)
{
var other = _options.Where(p => p.Key != a).Select(p => p.Key).ToArray();
foreach (string key in other)
_options[key] = false;
_options[a] = true;
RaisePropertyChanged("XXXX");
else
_options[a] = false;
}
}
}
XAML
<RadioButton Content="Day" IsChecked="{Binding RadioSwitch[radio1], Mode=TwoWay}" GroupName="Monthly" HorizontalAlignment="Left" VerticalAlignment="Center" />
View 模型
RadioSwitch = new RadioButtonSwitch(
new Dictionary<string, bool> {{"radio1", true},{"radio2", false}}
);
我的类(class)中的 RaisePropertyChanged() 遇到问题。我不确定我应该投入什么值(value)来提高变革。
我尝试放置:
- 项目[]
- 一个
- [a]
我不断收到以下错误:
这样,如果发生任何变化,我可以在我的观点中相应地处理它。请不要给我单选按钮列表等的解决方案。
最佳答案
问题在于您正在实现一个索引器,而不是一个普通的属性。尽管绑定(bind)子系统支持索引器,MVVMLight 和 INotifyPropertyChanged
不。
如果您想使用索引器,您需要:
- 使用集合基类,例如
ObservableCollection<T>
- 实现
INotifiyCollectionChanged
并引发那个事件
第一个选项不现实,因为您已经派生自 ViewModelBase
并且必须继续这样做。自实现INotifiyCollectionChanged
以来需要一点工作,最简单的方法是:
- 将属性添加到
RadioButtonSwitch
这是一个可观察的 bool 值集合 (ObservableCollection<bool>
)
然后更改您的绑定(bind)以再添加一个路径元素,您就完成了。
编辑:
根据您的评论并重新阅读您的问题,我认为实现 INotifyCollectionChanged
是最简单的。这是您的 RadioButtonSwitch
的重写实际上不再需要从 MVVMLight 基类派生,尽管如果您愿意,您仍然可以这样做。
细心的读者会注意到,当集合的任何元素被修改时,我们使用大锤并“重置”整个集合。这不仅仅是懒惰,而是懒惰。这是因为索引器使用字符串索引而不是整数索引,并且 INotifyCollectionChanged
不支持这一点。因此,当发生任何变化时,我们只是举手并说整个系列已经改变。
public class RadioButtonSwitch : INotifyCollectionChanged
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected void RaiseCollectionChanged()
{
if (CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
IDictionary<string, bool> _options;
public RadioButtonSwitch(IDictionary<string, bool> options)
{
this._options = options;
}
public bool this[string a]
{
get
{
return _options[a];
}
set
{
if (value)
{
var other = _options.Where(p => p.Key != a).Select(p => p.Key).ToArray();
foreach (string key in other)
_options[key] = false;
_options[a] = true;
RaiseCollectionChanged();
}
else
_options[a] = false;
}
}
}
关于C# - 动态属性和 RaisePropertyChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6079224/