[英]Why can I not populate an array in my back-end C# that I declare in my ViewModel?
I have this code in my VM and it works okay: 我的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" },
};
I changed it to this as I think I should not populate data in the VM but it seems not to work as expected: 我将其更改为此,因为我认为我不应该在VM中填充数据,但似乎无法按预期工作:
VM VM
public ParamViewModel[] CardChoice { get; set; }
C# back-end 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" }
};
But now nothing appears in the controls that use this data as back-end. 但是现在在使用此数据作为后端的控件中没有任何内容。 Is there a problem with the way I am populating the data in the back-end? 我在后端填充数据的方式是否有问题?
Change your VM code like below. 如下更改您的VM代码。
When you assing the property on a later stage than the UI is rendered then you have to use the INotifyPropertyChanged
to tell the Renderer to rerender 当您在呈现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")
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.