什么相当于windows phone 8.1(Xaml)中android的listview适配器

时间:2015-10-19 18:18:35

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

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

1 个答案:

答案 0 :(得分: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将传递为&#34;值&#34;转换函数中的参数,转换函数将返回格式化的字符串,以便在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

相关问题