【问题标题】:what is equivalent of listview adapter of android in windows phone 8.1(Xaml)windows phone 8.1(Xaml)中android的listview适配器等效于什么
【发布时间】:2015-10-19 18:18:35
【问题描述】:

我知道我们可以直接在 Xaml 中绑定属性。 但我的要求有点不同。我想要完全控制绑定数据。 所以我正在寻找适配器类型的方法。我想根据该项目的文本块中的行数显示一些元素。在这里我不能使用值转换器,因为那时我的 UI 还没有准备好,而且我找不到每个文本块的行数。

【问题讨论】:

标签: xaml listview windows-phone-8.1 winrt-xaml win-universal-app


【解决方案1】:

我将举例说明,假设我想在绑定期间将时间格式从 hh:mm:ss 更改为 hh:mm。

首先,我将创建实现 IValueConverter 的公共类。

例如:-

public class RemoveSecondsInTime : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        string date = value as string;

        date = date.Substring(0, date.Length - 3);

        return date;
    }

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

其次,要使用这个转换器类,我们需要在页面资源中添加这个转换器类。

例如:-

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

第三,我们将创建我们的 ListView,如下所示:-

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate >
            <TextBlock Text="{Binding TimeWithSeconds, Converter {StaticResource ChangeTimeFormat}}" />       
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

因此,TimeWithSeconds 将作为“值”参数传递给 Convert 函数,Convert 函数将返回格式化字符串以显示在 textBox 中。

参考:-

1) https://channel9.msdn.com/Series/Windows-Phone-8-1-Development-for-Absolute-Beginners/Part-25-Advanced-Binding-with-Value-Converters

2) https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.ivalueconverter

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 2014-12-08
    • 1970-01-01
    • 2015-05-23
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多