2

I know that we can directly bind properties in Xaml. But My requirement is bit different. I want full control on binding data. So I am looking for adapter type approach. I want to display some elements based on number of lines in textblock of that item. Here i cant use value converter because at that time my UI won't be ready and I cant find number of lines of each textblocks.

  • https://books.google.lk/books?id=y_PM40ZegSQC&pg=PA246&lpg=PA246&dq=what+is+equivalent+of+listview+adapter+of+android+in+windows+phone&source=bl&ots=2eNz_ZUfdF&sig=MKE59E5RHsqlTLypormf3ZkB5z0&hl=en&sa=X&ved=0ahUKEwiB5sCExoPKAhWBA44KHQWkBdoQ6AEIMTAE#v=onepage&q=what%20is%20equivalent%20of%20listview%20adapter%20of%20android%20in%20windows%20phone&f=false – Codeone Dec 30 '15 at 12:08

1 Answers1

1

I will explain with example, let's suppose I want to change time format from hh:mm:ss to hh:mm during binding.

First, I will create public class that implements IValueConverter.

for example :-

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();
    }
}

Second, To use this converter class, we need to add this converter class in page resources.

for example :-

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

Third, We will create create our ListView as following :-

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

So, TimeWithSeconds will pass as "value" parameter in Convert function, Convert function will return formatted string to be displayed in textBox.

References :-

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

Nitin Varun
  • 126
  • 7