如何从 ListView 中引用 ViewModel 属性作为转换器参数?

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

在我的演示 .NET MAUI 应用程序中,我有这个模型:

public class Person
{
    public string Name { get; set; }
    public string Email { get; set; }
}

这是我的视图模型:

public class MainViewModel : BaseViewModel
{
    private Person _selectedPerson;
    public Person SelectedPerson
    {
        get => _selectedPerson;
        set
        {
            if (_selectedPerson != value)
            {
                _selectedPerson = value;
                OnPropertyChanged(nameof(SelectedPerson));
                OnPropertyChanged(nameof(People));
            }
        }
    }

    public ObservableCollection<Person> People { get; } = new ObservableCollection<Person>
    {
        new Person { Name = "John Doe", Email = "[email protected]" },
        new Person { Name = "Jane Doe", Email = "[email protected]" },
        new Person { Name = "Bob Smith", Email = "[email protected]" },
        new Person { Name = "Alice Johnson", Email = "[email protected]" },
    };

    public ICommand SelectPersonCommand => new Command<Person>(person =>
    {
        SelectedPerson = person;
    });

    public MainViewModel()
    {

    }
}

我有可枚举的

People
绑定到我的
ListView

<ContentPage
    x:Class="MAUIPlayground.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:converters="clr-namespace:MAUIPlayground.Converters"
    x:Name="mainPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <converters:SelectedItemToBooleanConverter x:Key="SelectedItemToBooleanConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout>
        <ListView ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding ., Converter={StaticResource SelectedItemToBooleanConverter}, ConverterParameter={Binding Source={x:Reference mainPage}, Path=BindingContext.SelectedPerson}}" />
                            <Label Text="{Binding Name}" />
                            <Label Text="{Binding Email}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

这是我的 SelectedItemToBooleanConverter:

public class SelectedItemToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            var person = (Person)value;
            var selectedPerson = (Person)parameter;
            return person == selectedPerson;
        }

        return false;
    }

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

据我所知,我应该能够将转换器上的

value
parameter
参数解析为
People
。这对于
value
是正确的,但是当我试图将 Person 传递为
parameter
时,
Microsoft.Maui.Controls.Binding
被传递为
argument
。我怎样才能做到这一点?

.net xaml xamarin.forms data-binding maui
© www.soinside.com 2019 - 2024. All rights reserved.