سؤال

I'm trying to bind my ListPicker to the TimeSpan.Days property in an ObservableCollection, I'm also using a Converter to format the string, but I cannot get the binding to work. I don't get any errors, but the ListPicker does not have any items and the code in my converter does not get exexcuted(breakpoint never gets hit).

I have discovered that if I bind to the collection like this:

ItemsSource="{Binding Converter={StaticResource intervalConverter}}"

Then the code in my converter does get executed but the "value" parameter of the Convert function is the whole collection rather then a single item of the collection.

Can anyone explain why my binding won't work?

This is my XAML:

<toolkit:ListPicker x:Name="intervalPicker"
    ExpansionMode="ExpansionAllowed"
    ItemsSource="{Binding Days, Converter={StaticResource intervalConverter}}"/>

This is my code behind:

public partial class MainPage : PhoneApplicationPage
{
    ObservableCollection<TimeSpan> _intervals;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
        PopulatePicker();
    }

    private void PopulatePicker()
    {
        //Insulin Interval Picker
        _intervals = new ObservableCollection<TimeSpan>();
        _intervals.Add(new TimeSpan(1, 0, 0, 0));
        _intervals.Add(new TimeSpan(2, 0, 0, 0));
        _intervals.Add(new TimeSpan(3, 0, 0, 0));

        intervalPicker.DataContext = _intervals;
    }
}

public class IntervalConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int interval = (int)value;
        if(interval > 1)
            return string.Format("{0 days}", interval);
        else
            return string.Format("{0 day}", interval);
    }
}
هل كانت مفيدة؟

المحلول

You need to move the converter to the ItemTemplate for the ListPicker. If you use it with the ItemsSource binding then you are telling the framework that you need to convert the value being used for the items collection (this is why the whole collection was being passed to your converter).

EDIT: The converter code needs to be changed as well. The format string should be "{0} days" not "{0 days}"

Here's some xaml that should be close to what you want:

<toolkit:ListPicker x:Name="intervalPicker"
                    ExpansionMode="ExpansionAllowed"
                    ItemsSource="{Binding}">
    <toolkit:ListPicker.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Days, Converter={StaticResource intervalConverter}"/>
        </DataTemplate>
    </toolkit:ListPicker.ItemTemplate>
    <toolkit:ListPicker.FullModeItemTemplate>
        <DataTemplate>
            <TextBlock Style={StaticResource PhoneTextTitle2Style}"
                       Text="{Binding Path=Days, Converter={StaticResource intervalConverter}"/>
        </DataTemplate>
    </toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top