Thunderburst
Thunderburst

Reputation: 35

Binding command on behavior with a converter

I'm working on a Xamarin.Forms project.

I have a behavior on a listview, which is doing a binding on a command with a converter. I did it with XAML and C# and it's working perfectly.

XAML part :

<ListView.Behaviors>
  <bh:ListViewPagingBehavior
    Command="{Binding LoadMoreLeadOfTheDateCommand}"
    Converter="{StaticResource ItemVisibilityConverter}">
  </bh:ListViewPagingBehavior>
</ListView.Behaviors>

But now i need to do this process on code-behind only, cause i needed to create my listview in the code-behind.

I tried to traduce this XAML like this :

ListViewPagingBehavior behavior = new ListViewPagingBehavior();
behavior.SetBinding(ListViewPagingBehavior.CommandProperty, "LoadMoreLeadOfTheDateCommand", BindingMode.Default, new ItemVisibilityEventArgsConverter());
myListView.Behaviors.Add(behavior);

Unfortunatly, the IValueConverter doesn't retrieve the same parameters as before on the Convert() method...

My Converter :

public class ItemVisibilityEventArgsConverter : IValueConverter
{
    public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
    {
        var eventArgs = value as ItemVisibilityEventArgs;
        return eventArgs.Item;
    }

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

Good parameters of Convert() with the working code :

Bad parameters of Convert() with my all C# code :

Can someone tell me where am i wrong ? Thanks a lot !

Upvotes: 0

Views: 1104

Answers (2)

Stephane Delcroix
Stephane Delcroix

Reputation: 16222

I have no idea what ListViewPagingBehavior so I can only guess, but looking at this syntax:

<ListView.Behaviors>
  <bh:ListViewPagingBehavior
    Command="{Binding LoadMoreLeadOfTheDateCommand}"
    Converter="{StaticResource ItemVisibilityConverter}">
  </bh:ListViewPagingBehavior>
</ListView.Behaviors>

the Converter is a property of the ListViewPagingBehavior and not a property of the {Binding} (having a visibility converter to converter would seem very strange, on the other hand).

The equivalent C# for this would look like this:

var behavior = new ListViewPageBindingBehavior();
behavior.SetBinding(ListViewPageBindingBehavior.CommandProperty, "LoadMoreLeadOfTheDateCommand");
behavior.Converter = new ItemVisibilityConverter();

myListView.Behaviors.Add (behavior);

Upvotes: 0

maddhew
maddhew

Reputation: 54

I'm not sure if I've reproduced your case well enough, but here's the thing I came up with.

behavior.SetBinding(ListViewPagingBehavior.CommandProperty, "LoadMoreLeadOfTheDateCommand", BindingMode.Default, new ItemVisibilityEventArgsConverter());

Here you're using your converter to convert what you're binding, and that means you're converting "LoadMoreLeadOfTheDateCommand". That's why you get DelegateCommand as value. Your binding should look like this:

behavior.SetBinding(ListViewPagingBehavior.CommandProperty, "LoadMoreLeadOfTheDateCommand");

And then just use your behavior's ConverterProperty (or whatever you've called it):

behavior.Converter = new ItemVisibilityEventArgsConverter();

By the way that is what you're doing in your XAML. Anyway that works for me and I hope it'll work for you also :)

Upvotes: 0

Related Questions