我有一个列表视图,该列表视图的数据模板在该卷帘控件中有一个Swipe Control和一个文本块。现在,在卷帘控件的右侧项目中有3个项目,例如: 1.添加2.编辑3.删除
我想根据条件显示正确的项目。如果文本块没有字符串,那么在向右滑动时,只显示"Add“。如果文本块中存在字符串,则在滑动时显示其他2个字符串。
有没有办法在UWP中使用swipe控件来实现这一点?
发布于 2018-09-06 16:19:30
您可以使用绑定根据绑定到SwipeControl的Text属性的模型内容来更改Textblock的滑动项数。
下面是一个示例来说明这一点:
在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。
https://stackoverflow.com/questions/52187625
复制相似问题