我创建了一个要在列表视图中使用的自定义控件。viewProgress除了根据我的订单状态返回布尔值的自定义属性外,它几乎可以正常工作。尽管状态正确地获取了他的值,但布尔值永远不会被重新读取,因此绑定永远不会收到真值。我尝试使用断点来检测是否OnPropertyChanged正在运行,但它似乎永远不会到达那里。Item即使数据已绑定,该属性似乎也永远不会设置。我究竟做错了什么?
内容视图:
<?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>