首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UWP滑动控件-基于条件的列表项

UWP滑动控件-基于条件的列表项
EN

Stack Overflow用户
提问于 2018-09-05 22:30:24
回答 1查看 337关注 0票数 1

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

我想根据条件显示正确的项目。如果文本块没有字符串,那么在向右滑动时,只显示"Add“。如果文本块中存在字符串,则在滑动时显示其他2个字符串。

有没有办法在UWP中使用swipe控件来实现这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-06 16:19:30

您可以使用绑定根据绑定到SwipeControlText属性的模型内容来更改Textblock的滑动项数。

下面是一个示例来说明这一点:

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

代码语言:javascript
运行
复制
<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类:

代码语言:javascript
运行
复制
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,

代码语言:javascript
运行
复制
<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,

代码语言:javascript
运行
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52187625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档