我的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中填充数据,但似乎无法按预期工作:
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" }
};
但是现在在使用此数据作为后端的控件中没有任何内容。我在后端填充数据的方式有问题吗?
答案 0 :(得分:1)
按如下所示更改您的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")
}
}
}