我的 VM 中有此代码,它运行正常:public ParamViewModel[] CardChoice { get; set; } = new[] { new ParamViewModel { Id = 0, Name = CC.All.ShortText(), IsSelected = false, BackgroundColor="#FFFFFF", TextColor="#999999", BorderColor="#999999" }, new ParamViewModel { Id = 1, Name = CC.Catg.ShortText(), IsSelected = false, BackgroundColor="#FFFFFF", TextColor="#999999" , BorderColor="#999999" }, };我把它改成这样,因为我认为我不应该在 VM 中填充数据,但它似乎没有按预期工作:虚拟机public ParamViewModel[] CardChoice { get; set; }C# 后端vm.CardChoice = new[] { new ParamViewModel { Id = 0, Name = CC.All.ShortText(), IsSelected = false, BackgroundColor="#FFFFFF", TextColor="#999999" , BorderColor="#999999" }, new ParamViewModel { Id = 1, Name = CC.Catg.ShortText(), IsSelected = false, BackgroundColor="#FFFFFF", TextColor="#999999" , BorderColor="#999999" } };但是现在在使用这些数据作为后端的控件中什么也没有出现。我在后端填充数据的方式有问题吗?
1 回答
郎朗坤
TA贡献1921条经验 获得超9个赞
更改您的 VM 代码,如下所示。
当您在渲染 UI 之后的阶段分配属性时,您必须使用INotifyPropertyChanged来告诉渲染器重新渲染
public class YourVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
private ParamViewModel[] cardChoice;
public ParamViewModel[] CardChoice
{
get { return cardChoice; }
set
{
cardChoice = value;
OnPropertyChanged("CardChoice")
}
}
}
- 1 回答
- 0 关注
- 177 浏览
添加回答
举报
0/150
提交
取消