简体   繁体   English

将行为绑定到转换器的命令

[英]Binding command on behavior with a converter

I'm working on a Xamarin.Forms project. 我正在研究Xamarin.Forms项目。

I have a behavior on a listview, which is doing a binding on a command with a converter. 我在listview上有一个行为,该行为正在用转换器对命令进行绑定。 I did it with XAML and C# and it's working perfectly. 我用XAML和C#做到了,并且运行良好。

XAML part : XAML部分:

<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. 但是现在我只需要在代码隐藏中执行此过程,因为我需要在代码隐藏中创建我的listview。

I tried to traduce this XAML like this : 我试图像这样来介绍这个XAML:

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... 不幸的是,IValueConverter无法在Convert()方法上检索到与以前相同的参数。

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 : 使用工作代码的Convert()的好参数:

  • value = Xamarin.Forms.ItemVisibilityEventArgs 值= Xamarin.Forms.ItemVisibilityEventArgs

  • targetType = System.Object targetType = System.Object

  • parameter = null 参数=空

  • culture = null 文化=空

Bad parameters of Convert() with my all C# code : 使用我所有的C#代码的Convert()的参数错误:

  • value = DelegateCommand 值= DelegateCommand

  • targetType = ICommand targetType = ICommand

  • parameter = null 参数=空

  • culture = {fr-FR} 文化= {fr-FR}

Can someone tell me where am i wrong ? 有人可以告诉我我错了吗? Thanks a lot ! 非常感谢 !

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". 在这里,您正在使用转换器来转换要绑定的内容,这意味着您正在转换“ LoadMoreLeadOfTheDateCommand”。 That's why you get DelegateCommand as value. 这就是为什么将DelegateCommand作为值。 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): 然后只需使用您行为的ConverterProperty(或您所说的任何东西)即可:

behavior.Converter = new ItemVisibilityEventArgsConverter();

By the way that is what you're doing in your XAML. 顺便说一下,这就是您在XAML中所做的事情。 Anyway that works for me and I hope it'll work for you also :) 无论如何对我有用,我希望它也对你有用:)

I have no idea what ListViewPagingBehavior so I can only guess, but looking at this syntax: 我不知道什么是ListViewPagingBehavior所以我只能猜测,但是看下面的语法:

<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). ConverterListViewPagingBehavior一个属性,而不是{Binding}一个属性(另一方面,将可见性转换器转换为转换器似乎很奇怪)。

The equivalent C# for this would look like this: 与此等效的C#如下所示:

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

myListView.Behaviors.Add (behavior);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM