简体   繁体   English

为什么不能在ViewModel中声明的后端C#中填充数组?

[英]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.

相关问题 如何在Android应用程序中使用C#后端裁剪和调整图像大小? - How do I crop and resize images using C# back-end for my android application? 我可以在ViewModel中简化此C#吗? - Can I simplify this C# in my ViewModel? 如何使用从 .txt 文件中读取的对象填充我的列表? (C# UWP) - How can I populate back my list with objects read from .txt file? (C# UWP) 如何将DynamicResource的值发送到自定义控件C#后端? - How can I send the value of a DynamicResource to my custom controls C# back end? 如何在集合中填充viewmodel的属性? - how can i populate my viewmodel's property with collection in it? 为什么我的后端服务的前端调用被拒绝? - Why is my front-end call to my back-end service being refused? 我可以在 MVC 后端创建无状态 static class - Can I create a stateless static class in MVC back-end 从前端接收 GMT +0 日期字符串时,如何在后端使其成为 GMT +2 日期对象? - When receiving a GMT +0 date string from front-end, how do I make it a GMT +2 Date object in my back-end? 我已经将XML文件反序列化为class.cs,如何在c#ViewModel中使用它 - I have deserialized my XML file into class.cs, how can I use it in c# ViewModel 如何将对象数组从 Angular 前端传递到 C# 后端 - How to pass Array of Objects from Angular Front-end to C# Back-end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM