【发布时间】:2011-08-30 01:40:50
【问题描述】:
我有以下用于单选按钮绑定的类
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" />
视图模型
RadioSwitch = new RadioButtonSwitch(
new Dictionary<string, bool> {{"radio1", true},{"radio2", false}}
);
我在课堂上遇到了 RaisePropertyChanged() 问题。我不确定我应该投入什么价值来提高变革。
我试过把:
- 项目[]
- 一个
- [a]
我不断收到以下错误:
因此,如果有任何更改,我可以在我的视图中相应地处理它。请不要给我解决单选按钮列表等的解决方案。
【问题讨论】:
-
ViewModelBase定义在哪里? -
ViewModelBase扩展GalaSoft.MvvMLightViewModelBase。它实现了INotifyPropertyChanged。RaisePropertyChanged几乎只是打电话给if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(property)); -
嗯,那么错误来来自哪里?你能发布完整的堆栈跟踪吗?
-
@KirkWoll 添加了错误的屏幕截图。我假设正在发生错误,因为我没有使用正确的参数调用 RaisePropertyChanged()。
RadioButton绑定到[radio1],我尝试使用它作为属性名称,但它也不起作用。
标签: c# binding dynamic-properties