我一直在尝试创建一个数据绑定来访问父堆栈布局中的值。
我创建了一个转换器,认为这可以让我了解我的绑定在做什么。
但是,我只是得到一个空参数值——我需要设置为父堆栈布局中字段的文本值?
有 .. 必须 .. 一些技术来智能调试绑定并了解如何创建正确的绑定?
我已经尝试创建一个标签来给我一些关于绑定正在做什么的线索 - 尝试了以下标签的许多变体但是..我收效甚微?
<Label Text="{Binding Source={x:Reference parentView}, StringFormat='{0}'}" />
这是我的转换器,其中参数始终是 mull.
// changing ConverterParameter={Binding CustId}}"> to ConverterParameter={Binding InvHeader.CustId}}">
// just changes the PATH in parameter to PATH InvHeader.CustId
// we are always passing null??
// value shows as 39 InvItems
// trying a few variations, all of them return null in parameter
// <StackLayout BindableLayout.ItemsSource="{Binding InvItems, Converter={StaticResource InvItemsForCustIdConverter}, ConverterParameter={Binding InvHeader.CustId}}">
// <StackLayout BindableLayout.ItemsSource="{Binding InvItems, Converter={StaticResource InvItemsForCustIdConverter}, ConverterParameter={Binding CustId}}">
// <StackLayout BindableLayout.ItemsSource="{Binding InvItems, Converter={StaticResource InvItemsForCustIdConverter}, ConverterParameter={Binding CustId, Source={x:Reference parentView}, Path=InvHeader}}">
public class InvItemsForCustIdConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value; // If I return the invItems then the display works, but does not Limit the InvItems to those matching the InvHeader StackLayout
if (value is List<InvItem> invItems && parameter is string custId)
{
return invItems.Where(x => x.CustId == custId).ToList();
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是我正在尝试调试的嵌套 XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Name="parentView"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converters="clr-namespace:MauiClient"
x:Class="MauiClient.Invoice"
Title="Invoice">
<ContentPage.Resources>
<ResourceDictionary>
<converters:InvItemsForCustIdConverter x:Key="InvItemsForCustIdConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ScrollView>
<StackLayout>
<!-- Header section -->
<Label Text="Hospitals" FontSize="Title" />
<CollectionView ItemsSource="{Binding invHeaders}" BackgroundColor="Blue">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Company}" />
<Label Text="{Binding Addr1}" />
<Label Text="{Binding Phone}" />
<Label Text="{Binding CustId}" BackgroundColor="Red" TextColor="White" />
<!-- Invoice items section -->
<Label Text="Tech Times" FontSize="Title" BackgroundColor="Yellow" TextColor="Black"/>
<StackLayout BindingContext="{Binding BindingContext, Source={x:Reference parentView}}" BackgroundColor="Green">
<StackLayout BindableLayout.ItemsSource="{Binding InvItems, Converter={StaticResource InvItemsForCustIdConverter}, ConverterParameter={Binding CustId}}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal" Spacing="20">
<Label Text="{Binding ItemId}" />
<Label Text="{Binding CustId}" BackgroundColor="Red" TextColor="White" />
<Label Text="{Binding Source={x:Reference parentView}, StringFormat='{0}'}" />
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</StackLayout>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ScrollView>
</ContentPage>
这是基本的 ViewModel:
public class InvoiceViewModel {
public List<InvHeader> InvHeaders { get; set; }
public List<InvItem> InvItems { get; set; }
}