【问题标题】:UWP, Apply a converter to a ListView's item when the ListView's items source is a collection of stringsUWP,当 ListView 的项目源是字符串集合时,将转换器应用于 ListView 的项目
【发布时间】:2018-02-20 06:39:19
【问题描述】:

我有以下情况。我想显示一个字符串列表。为了实现这一点,我有一个绑定到字符串集合的 ListView。在这个集合中,有一些空字符串。我想要的是在存在空字符串时显示以下文本:“-empty-”。这是我目前得到的(源代码仅用于演示目的):

EmptyStringConverter.cs

public class EmptyStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value is string && string.IsNullOrWhiteSpace((string)value))
        {
            return "-empty-";
        }
        return value;
    }

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

MainPage.xaml

<Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">

    <Page.Resources>
        <local:EmptyStringConverter x:Key="EmptyStringConverter" />
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <ListView x:Name="ListView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Converter={StaticResource EmptyStringConverter}}" Margin="0,0,0,5" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>

MainPage.xaml.cs

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        var source = new[] { "", "String 1", "String 2" };
        ListView.ItemsSource = source;
    }
}

当在 EmptyStringConverter 类的 Convert 方法中放置断点时,该方法会在除空字符串之外的每个项目中调用。我怎样才能实现我想要的?

【问题讨论】:

    标签: string listview uwp converter


    【解决方案1】:

    好吧,问题出在我最后要检查的地方。是你的Legacy Binding 导致了这个问题。我自己尝试了一个代码 sn-p 然后我用你的代码一块一块地替换它。下面的行使用legacy binding

    Text="{Binding Converter={StaticResource EmptyStringConverter}}"

    由于您使用的是UWP,因此您可以切换到Compile time binding,这将解决您的问题,您修改后的ListView XAML 将是:

    <ListView x:Name="ListView" >
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="x:String">
                    <TextBlock Text="{x:Bind Converter={StaticResource EmptyStringConverter},Mode=OneTime}" Margin="0,0,0,5" />
                </DataTemplate>
            </ListView.ItemTemplate>
    </ListView>
    

    请注意两点:

    1. DataTemplateTextboxText 部分,它使用x:Bind 而不是Binding。请注意,编译时绑定默认为oneTime 绑定(除非您明确提及模式),而旧版Binding 默认为OneWay 绑定。有关编译时绑定的更多信息,请关注:xBind markup extension
    2. 如果您注意到DataTemplate 声明包含DataType 属性,这有助于编译时绑定器了解它期望的数据类型。有关DataType 的更多信息,请关注:xBind markup extension

    话虽如此,我强烈建议使用Data Binding 方法而不是ListView.ItemSource=source,因为与新的编译时绑定一样,绑定引擎本身会处理大量转换,从而减少您的代码和工作量结尾。我已经在 github 上放了一个示例,您可以查看:EmptyStringDemo

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 2011-12-05
      • 1970-01-01
      • 2019-01-15
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2020-10-22
      相关资源
      最近更新 更多