UWP滑动控制 - 根据条件列出项目

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

我有一个列表视图,该列表视图的数据模板在该滑动控件中有一个Swipe Control和一个文本块。现在,滑动控件的右项中有3个项目,例如:1。添加2.编辑3.删除

我想根据条件显示正确的项目。如果Textblock没有字符串,则在向右滑动时,仅显示“Add”。如果Textblock中存在字符串,则在滑动时显示另一个2。

有没有办法通过滑动控制在UWP中实现这一点?

uwp swipe uwp-xaml swipe-gesture
1个回答
0
投票

您可以使用绑定来更改SwipeControl的滑动项目编号,该编号基于模型的内容,该内容绑定到TextblockText属性。

以下是澄清它的示例:

在App.xaml中,添加SwipeItems资源,

<Application.Resources>
    <SymbolIconSource x:Key="AddIcon" Symbol="Add"/>
    <SymbolIconSource x:Key="EditIcon" Symbol="Edit"/>
    <SymbolIconSource x:Key="DeleteIcon" Symbol="Delete"/>

    <SwipeItems x:Key="ThreeItems" Mode="Reveal">
        <SwipeItem Text="Edit" IconSource="{StaticResource EditIcon}"/>
        <SwipeItem Text="Delete" IconSource="{StaticResource DeleteIcon}"/>
        <SwipeItem Text="Add"  IconSource="{StaticResource AddIcon}"/>
    </SwipeItems>

    <SwipeItems x:Key="OneItem" Mode="Reveal">
        <SwipeItem Text="Add"  IconSource="{StaticResource AddIcon}"/>
    </SwipeItems>
</Application.Resources>

这是绑定到ListView项的Model类:

public class Model
{
    public string Content { get; set; }
    public SwipeItems Swips
    {
        get
        {
            if (string.IsNullOrEmpty(Content))
            {
                return (SwipeItems)Application.Current.Resources["OneItem"];
            }
            else
            {
                return (SwipeItems)Application.Current.Resources["ThreeItems"];
            }
        }
    }
}

在MainPage.xaml中,有一个ListView,

<ListView x:Name="sampleList" ItemsSource="{x:Bind ListSource}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:Model">
            <SwipeControl x:Name="ListViewSwipeContainer"
                      LeftItems="{x:Bind Swips}"
                      Height="60">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{x:Bind Content}" FontSize="18"/>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Text Item details" FontSize="12"/>
                    </StackPanel>
                </StackPanel>
            </SwipeControl>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

这是MainPage.xaml.cs,

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        ListSource = new ObservableCollection<Model>();
        ListSource.Add(new Model { Content = "first" });
        ListSource.Add(new Model { });
        ListSource.Add(new Model { Content = "Second" });
        ListSource.Add(new Model { Content = "Third" });
    }
    public ObservableCollection<Model> ListSource;
}

另一方面,您还可以通过使用ListView的ItemTemplateSelector属性来引用不同的ListView ItemTemplate来实现此效果。你需要实现自己的DataTemplateSelector,根据模型的内容是空还是空来返回相应的DataTemplate

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