首先,感谢你对我上一个问题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 = "";
}
}
}