SelectedItemEventArgsConverter
SelectedItemEventArgsConverter
是一个允许用户从 SelectedItemChangedEventArgs
对象中提取 SelectedItem 值的转换器。 它随后可与 EventToCommandBehavior 结合使用。
BaseConverter 属性
以下属性是在基类 public abstract class BaseConverter
中实现的:
ICommunityToolkitValueConverter 属性
以下属性在 public interface ICommunityToolkitValueConverter
中实现:
语法
XAML
包括 XAML 命名空间
若要在 XAML 中使用工具包,需要将以下 xmlns
添加到页面或视图中:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
因此,以下内容:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
</ContentPage>
将被修改为包括 xmlns
,如下所示:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
</ContentPage>
使用 SelectedItemEventArgsConverter
SelectedItemEventArgsConverter
可以在 XAML 中按如下所示方式使用:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="MyLittleApp.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<toolkit:SelectedItemEventArgsConverter x:Key="SelectedItemEventArgsConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<VerticalStackLayout Padding="10">
<Label
Text="The SelectedItemEventArgsConverter is a converter that allows users to extract the SelectedItem value from an SelectedItemChangedEventArgs object. It can subsequently be used in combination with EventToCommandBehavior."
TextColor="{StaticResource NormalLabelTextColor}"
Margin="0, 0, 0, 20" />
<ListView
BackgroundColor="Transparent"
ItemsSource="{Binding Items}"
SelectedItem="{Binding ItemSelected, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<VerticalStackLayout Margin="6">
<Label Text="{Binding Name, StringFormat='Name: {0}'}"/>
</VerticalStackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Behaviors>
<toolkit:EventToCommandBehavior EventName="ItemSelected"
Command="{Binding ItemSelectedCommand}"
EventArgsConverter="{StaticResource SelectedItemEventArgsConverter}" />
</ListView.Behaviors>
</ListView>
</VerticalStackLayout>
</ContentPage>
C#
SelectedItemEventArgsConverter
可在 C# 中按如下所示方式使用:
class SelectedItemEventArgsConverterPage : ContentPage
{
public SelectedItemEventArgsConverterPage()
{
var behavior = new EventToCommandBehavior
{
EventName = nameof(ListView.ItemSelected),
EventArgsConverter = new SelectedItemEventArgsConverter()
};
behavior.SetBinding(EventToCommandBehavior.CommandProperty, static (ViewModel vm) => vm.ItemSelectedCommand);
var listView = new ListView
{
HasUnevenRows = true
};
listView.SetBinding(ListView.ItemsSource, static (ViewModel vm) => vm.Items);
listView.Behaviors.Add(behavior);
Content = listView;
}
}
C# 标记
我们的 CommunityToolkit.Maui.Markup
包提供了在 C# 中使用此转换器的更简洁的方法。
using CommunityToolkit.Maui.Markup;
class SelectedItemEventArgsConverterPage : ContentPage
{
public SelectedItemEventArgsConverterPage()
{
Content = new ListView
{
HasUnevenRows = true
}
.Bind(
ListView.ItemsSourceProperty,
static (ViewModel vm) => vm.Items)
.Behaviors(
new EventToCommandBehavior
{
EventName = nameof(ListView.ItemSelected),
EventArgsConverter = new SelectedItemEventArgsConverter()
}
.Bind(
EventToCommandBehavior.CommandProperty,
static (ViewModel vm) => vm.ItemTappedCommand));
}
}
示例
可以在 .NET MAUI 社区工具包示例应用程序中找到此转换器的示例。
API
可以在 .NET MAUI 社区工具包 GitHub 存储库查看SelectedItemEventArgsConverter
的源代码