How to send Maui XAML Bound value to parameter in code-behind C# Converter?

问题描述 投票:0回答:1

我需要绑定到 Maui XAML 中的几个自定义转换器,这些转换器以非常复杂的方式简单地格式化字符串。

我在 C# 代码隐藏中创建了一个测试转换器,可以看到它被调用,但我无法获取包含 BegTime 内值的参数,只有字符串或路径?

我的转换器被调用,但参数永远不是实际的绑定值。

我可以向转换器发送固定字符串“BegTime”(不是BegTime的值)

<Label Text="{Binding BegTime, 
    Converter={StaticResource FormatJobTimeBegTime},
    ConverterParameter=BegTime}" />

我可以向它发送一个((Microsoft.Maui.Controls.Binding)参数)。路径包含“BegTime”(但不是 BegTime 的值)。

<Label Text="{Binding BegTime, 
    Converter={StaticResource FormatJobTimeBegTime},
    ConverterParameter={Binding BegTime}}" />

但我似乎无法向参数发送 Inside BegTime 的实际数据(包含时间的字符串)??

这是代码隐藏转换器的 XAML 声明:

<ContentPage.Resources>
    <ResourceDictionary>
        <converters:FormatJobTimeBegTimeConverter x:Key="FormatJobTimeBegTime" />
    </ResourceDictionary>
</ContentPage.Resources>

和 FormatJobTimeBegTimeConverter 被调用,因为我可以设置断点并观察参数

public class FormatJobTimeBegTimeConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        // I can set a breakpoint here to inspect the value of the parameter
        //parameter must contain a string with the value Inside BegTime???

        return "mm/dd/yy";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
         throw new NotImplementedException();
    }
}

BegTime 是类中的一个属性:

public sealed partial class TicketItem: ObservableObject {
    [ObservableProperty]
    public DateTime begTime; 
    [ObservableProperty]
    public DateTime endTime; 
 }

 public sealed partial class WhereSchedClient : ObservableObject {
    [ObservableProperty]
    public string company; 

    [ObservableProperty]
    public List<TicketItem> ticketItems;
 }
public class InvoiceViewModel  {
    public List<WhereSchedClient> WhereSchedClients { get; set; }
}

我在 XAML 中显示时间完全没有问题,例如:

<Label Text="{Binding BegTime}" />

这是完美运行的 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:MauiSched"
             xmlns:viewModels="clr-namespace:MauiSched"
             x:Class="MauiSched.Schedule"
             Title="Schedule">
    <ContentPage.Resources>
        <ResourceDictionary>
            <converters:FormatJobTimeBegTimeConverter x:Key="FormatJobTimeBegTime" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <ScrollView>
        <StackLayout>
            <!-- Header section -->
            <CollectionView ItemsSource="{Binding WhereSchedClients}" BackgroundColor="Blue">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Label Text="{Binding Company}" />
                            <!-- Invoice items section -->
                            <StackLayout BackgroundColor="Green">
                                <StackLayout BindableLayout.ItemsSource="{Binding TicketItems}">
                                    <BindableLayout.ItemTemplate>
                                        <DataTemplate>
                                            <StackLayout Orientation="Horizontal" Spacing="20">
                                                <Label Text="{Binding BegTime, 
                                                    Converter={StaticResource FormatJobTimeBegTime},
                                                    ConverterParameter={Binding BegTime}}" />
                                                <Label Text="{Binding BegTime}" />          
                                            </StackLayout>
                                        </DataTemplate>
                                    </BindableLayout.ItemTemplate>
                                </StackLayout>
                            </StackLayout>
                        </StackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </StackLayout>
    </ScrollView>
</ContentPage>

我遇到的麻烦是将 BegTime 中的值(这是一个日期字符串)作为参数传递给 FormatJobTimeBegTimeConverter,以便它可以正确格式化。

c# xaml data-binding converters maui
1个回答
0
投票

如果你使用

<Label Text="{Binding BegTime, Converter={StaticResource FormatJobTimeBegTime}}"
BegTime可以在没有ConverterParameter的情况下传递给转换器。

在 OneWay 或 TwoWay 绑定中,当数据从源移动到目标时,将调用 Convert 方法。值参数是来自数据绑定源的对象或值。

更多信息,您可以参考绑定值转换器

看下面的代码,BegTime 将传递给第一个 value 对象而不是 parameter 对象,正如 Jason 提到的那样。

public class FormatJobTimeBegTimeConverter : IValueConverter
{
    public FormatJobTimeBegTimeConverter()
    {
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime dt = (DateTime)value;
        return string.Format("{0}/{1}/{2}",dt.Month,dt.Day,dt.Year);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这对我来说效果很好。

顺便说一下,ConverterParameter 不是 BindableObject,因此您不能对其使用绑定。

希望它有效。

© www.soinside.com 2019 - 2024. All rights reserved.