首先,感谢你对我上一个问题here的解答

该解决方案效果很好,但是,我正在努力绑定其中一个属性的更改.当用户界面加载以下代码时,我的背景 colored颜色 绑定良好:

<StackLayout x:Name="stlShippingOrders" Spacing="5" Padding="0,0,0,5" >
        <CollectionView Margin="0,-6,0,0" IsGrouped="True" ItemsSource="{Binding 
ShippingItems}">
            <CollectionView.GroupHeaderTemplate>
                <DataTemplate>
                    <StackLayout Padding="5,-5,5,-5" Orientation="Horizontal" 
BackgroundColor="LightBlue" HeightRequest="45">
                        <Label Text="{Binding Shipment.OrderCode}" 
VerticalOptions="Center"/>
                        <Label Text="{Binding Shipment.CustomerName}" 
VerticalOptions="Center"/>
                        <Label Text="{Binding Shipment.DeliveryDate}" 
HorizontalOptions="EndAndExpand" VerticalOptions="Center" />
                    </StackLayout>
                </DataTemplate>
            </CollectionView.GroupHeaderTemplate>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Label Padding="5,2,5,2" Text="{Binding ItemDescription}" 
BackgroundColor="{Binding BackgroundColour}"/>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>

ShippingItem Model (with added BacgroundColour property)

 public class ShippingItem
{
    public String ShippingItemId { get; set; }
    public string ShippingItemScanCode { get; set; }
    public string ItemDescription { get; set; }
    public string ShippingOrderId { get; set; }
    public bool IsLoaded { get; set; }

    // Default set to transparent #00FFFFFF
    public string BackgroundColour { get; set; }  = "#00FFFFFF";

}

Class for grouping

public class ShippingItemGroup : ObservableCollection<ShippingItem>
{
    public ShippingOrder Shipment { get; private set; }

    public ShippingItemGroup(ShippingOrder shipment, ObservableCollection<ShippingItem> 
shippingItems) : base(shippingItems)
    {
        Shipment = shipment;
    }
}

ViewModel code that creates the group list and binds correctly

                        public ObservableCollection<ShippingItemGroup> ShippingItems { 
get; private set; } = new ObservableCollection<ShippingItemGroup>();
                    foreach (var item in thisShipment.ShippingOrders)
                    {
                        ShippingItems.Add(new ShippingItemGroup(item, 
item.ShippingItems));
                    }

ViewModel code that updates the list object when it is loaded. This is the bit i'm having trouble with, the shippingItem.BackgroundColour = "FFA533" changes the collection but doesn't update the UI.

foreach (ShippingItemGroup shippingItemGroup in ShippingItems)
                    {
                        foreach(ShippingItem shippingItem in 
shippingItemGroup.Shipment.ShippingItems)
                        {
                            if(shippingItem.ShippingItemScanCode == scanCode)
                            {
                                shippingItem.IsLoaded = true;
                                shippingItem.BackgroundColour = "FFA533";
                                IsShipmentHeaderVisible = true;
                                IsShippingOrdersVisible = true;
                                IsErrorMessageVisible = false;
                                ScanCode = "";
                            }
                        }
                    }

推荐答案

正如Jason在 comments 中提到的,您需要实现INotifyPropertyChanged.这是我为其他项目所做的.这是根据MVVMCross的操作方式改编的.

首先,创建一个基类,其中设置了INotifyPropertyChanged逻辑:

public class ModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(storage, value))
        {
            return false;
        }

        storage = value;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        return true;
    }
}

这将处理设置值和触发PropertyChanged事件.

接下来,从这个基类继承您需要的任何模型.在这里,它是您的ShippingItem类:

public class ShippingItem : ModelBase
{
    public int Id { get; set; }
    public int ShippingOrderId { get; set; }
    public string ItemDescription { get; set; }
    public bool IsLoaded { get; set; }

    private string backgroundColor;
    public string BackgroundColor
    {
        get => backgroundColor;
        set => SetProperty(ref backgroundColor, value);
    }
}

注意,添加了私有backgroundColor字段和在基类中调用SetProperty的自定义setter.

这将为您处理更新.

我修改了previous answer的代码,作为概念证明.这将每1.5秒更新一次 colored颜色 :

public MainPage()
{
    InitializeComponent();

    var shipment = new Shipment();

    foreach (var item in shipment.ShippingOrders)
    {
        ShippingItems.Add(new ShippingItemGroup(item, item.ShippingItems));
    }

    BindingContext = this;

    var rnd = new Random();
    Task.Run(async () =>
    {
        while (true)
        {
            await Task.Delay(1500);
            foreach (var shippingItemGroup in ShippingItems)
            {
                foreach (var shippingItem in shippingItemGroup)
                {
                   shippingItem.BackgroundColor = Color.FromRgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)).ToHex();
                }
            }
       }
    });
}

public ObservableCollection<ShippingItemGroup> ShippingItems { get; private set; } = new ObservableCollection<ShippingItemGroup>();

enter image description here

Csharp相关问答推荐

C# Json重新初始化动态类型

需要更改哪些内容才能修复被覆盖的财产中的无效警告CS 8765?

ASP.NET MVC购物车数量更新并从购物车中删除项目

MongoDB将JS查询转换为C#的问题

在. NET Core 8 Web API中,当为服务总线使用通用消费者时,如何防止IServiceProvider被释放或空?"

WPF Windows初始化正在锁定. Net 8中分离的线程

我需要两个属性类吗

.NET 6控制台应用程序,RabbitMQ消费不工作时,它的程序文件中的S

try 在.Net核心身份注册页面中使用AJAX,但没有成功..NET Core 5.0 Razor页面应用程序

关于扩展文件类C#的矛盾

Blazor Fluent UI DialogService,<;FluentDialogProvider/>;错误

为什么此名称不再被识别?名称不存在于当前上下文中?

如何在同一成员上组合[JsonPropertyName]和[ObservableProperty]?

如何在mediatr命令中访问HttpContext而不安装弃用的nuget包

如何强制新设置在Unity中工作?

如何使用IHostedService添加数据种子方法

如何读取TagHelper属性的文本值?

ASP.NET核心8:app.UseStaticFiles()管道执行顺序

与另一个对象位于同一位置的对象具有不同的变换位置

无法对包含字符串的列进行排序.请与实体框架联接