ContentView不更新绑定属性



我创建了一个用于列表视图的自定义控件。除了自定义属性viewProgress返回一个基于我的订单状态的布尔值外,它几乎工作得很好。尽管状态正确地获取了他的值,但布尔值永远不会被重新读取,因此绑定永远不会接收到真值。我尝试使用断点来检测OnPropertyChanged是否正在运行,但它似乎永远不会到达那里。Item属性似乎永远不会被设置,即使数据被绑定。我做错了什么?

ContentView:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:HandTerminal"
x:Class="HandTerminal.OrderItem">
<ContentView.Content>
<StackLayout>
<Frame
Margin="10,6,10,6"
Padding="10"
CornerRadius="10">
<StackLayout>
<StackLayout
Padding="0"
HorizontalOptions="Fill"
Orientation="Horizontal">
<Label
FontAttributes="Bold"
FontSize="Medium"
Text="Ordernumber"
TextColor="{DynamicResource TitleTextColor}" />
<Label
FontSize="Medium"
HorizontalOptions="FillAndExpand"
HorizontalTextAlignment="End"
Text="{Binding Item.Ordernumber}"
TextColor="{DynamicResource SubtleTextColor}" />
</StackLayout>
<StackLayout
IsVisible="{Binding viewProgress}"
Orientation="Horizontal"
HorizontalOptions="FillAndExpand">

<Label
HorizontalOptions="Start"
Text="Picked"
TextColor="{DynamicResource HighlightTextColor}" />
<ProgressBar 
HorizontalOptions="FillAndExpand"
Progress="{Binding Item.PickedProgress}" />
<StackLayout
HorizontalOptions="End"
Orientation="Horizontal"> 
<Label
Text="{Binding Item.TotalPicked}"
TextColor="{DynamicResource HighlightTextColor}" />
<Label
Text="/"
TextColor="{DynamicResource HighlightTextColor}" />
<Label
Text="{Binding Item.TotalAmount}"
TextColor="{DynamicResource HighlightTextColor}" />
</StackLayout>
</StackLayout>
</StackLayout>
</Frame>
</StackLayout>
</ContentView.Content>
</ContentView>

后台代码:

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace HandTerminal
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class OrderItem : ContentView
{
public OrderItem()
{
InitializeComponent();
Content.BindingContext = this;
}
public static BindableProperty ItemProperty = BindableProperty.Create(
propertyName: "Item",
returnType: typeof(Order),
declaringType: typeof(OrderItem),
defaultValue: new Order(),
defaultBindingMode: BindingMode.OneWay);
public Order Item
{
get
{
return (Order)GetValue(ItemProperty);
}
set
{
SetValue(ItemProperty, value);
OnPropertyChanged("Item");
OnPropertyChanged("viewProgress");
}
}

public static BindableProperty viewProgressProperty = BindableProperty.Create(
propertyName: "viewProgress",
returnType: typeof(bool),
declaringType: typeof(OrderItem),
defaultValue: false,
defaultBindingMode: BindingMode.OneWay);
public bool viewProgress
{
get
{
if (Item.Status == OrderStatus.Picking)
{
return true;
}
else
{
return false;
}
}
}
}
}

使用定制控件的ContentPage:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HandTerminal.Orders"
xmlns:controls="clr-namespace:HandTerminal"
xmlns:d="clr-namespace:HandTerminal;assembly=HandTerminal">
<ContentPage.Content>
<StackLayout
Orientation="Vertical"
VerticalOptions="CenterAndExpand">
<ListView
HasUnevenRows="True"
ItemsSource="{Binding Orders}"
VerticalOptions="StartAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<controls:OrderItem Item="{Binding .}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

</StackLayout>
</ContentPage.Content>
</ContentPage>

我试过你的代码,我的建议是:

  1. 创建viewProgressConverter

    public class viewProgressConverter : IValueConverter{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
    OrderStatus myOrder =(OrderStatus) value ;
    if (myOrder == OrderStatus.Picking)
    return true;
    else return false;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
    throw new NotImplementedException();
    }    }
    

你必须将它添加到App.Xaml中的AppResources

2。修改您的OrderItem视图,删除所有的"Item"。直接绑定到Order

<StackLayout
HorizontalOptions="FillAndExpand"
IsVisible="{Binding Status, Converter={StaticResource progressConverter}}"
Orientation="Horizontal">
  1. 您可以删除Content.BindingContext = this;和注释/删除所有可绑定的属性

最新更新