【问题标题】:Pass current model to as a ConverterParameter via xamarin binding通过 xamarin 绑定将当前模型作为 ConverterParameter 传递
【发布时间】:2017-12-15 02:05:56
【问题描述】:

在 ListView 的 DataTemplate 中写入 ConverterParameter={Binding} 会传递一个空的 Xamarin.Forms.Binding 对象而不是当前模型。

这是我的代码:

IsVisible="{Binding BindingContext.CardLoc, Source={x:Reference this} ,Converter={StaticResource MenuItemToIsVisibleReverseConverter} , ConverterParameter={Binding} "

除了ConverterParameter={Binding}之外,一切正常

【问题讨论】:

    标签: xamarin binding xamarin.forms


    【解决方案1】:

    这里是如何将命令参数传递给 viewmodel 和 view 。

    <ContentPage.Content>
        <ListView ItemsSource="{Binding Contacts}" ItemTapped="OnItemTapped">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell 
                        Text="{Binding FirstName}" 
                        Detail="{Binding LastName}" 
                    >
                        <TextCell.ContextActions>
                            <MenuItem 
                                Text="Apagar"
                                IsDestructive="true"
                                Command="{Binding Path=BindingContext.DeleteCommand, Source={x:Reference ContactView}}"
                                CommandParameter="{Binding .}"
                            />
                        </TextCell.ContextActions>
                    </TextCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
    

    【讨论】:

    • Nooooo,我的意思是 ConverterParameter 而不是 CommandParameter , CommandParameter 可以工作,但 ConverterParameter 不行。
    • 检查此链接是否适合您-forums.xamarin.com/discussion/71810/…
    • 标签作为转换器参数传递,但我写的时候它的文本属性为空: Text="{Binding Price}" ,价格是当前模型的属性,为什么?
    • 抱歉您的问题不清楚,请发送并编辑正确的问题
    • 我删除了 ConverterParameter 并找到了另一个没有它的解决方案。非常感谢您的回答,希望他们会在下一个版本中修复它。
    【解决方案2】:

    我相信您需要通过放置 . 来传递当前对象

    所以应该是ConverterParameter={Binding .}

    【讨论】:

    • 没办法,兄弟,没办法
    【解决方案3】:

    你必须通过引用传递:

    <Element
          Attribute="{Binding Value,
          Converter={StaticResource YourConverter},
          ConverterParameter={x:reference CompareTo}}" />
    

    【讨论】:

      【解决方案4】:

      很遗憾,Xamarin Forms 不支持 ConverterParameter 中的可绑定属性。一种解决方法是传递对页面上元素的引用并访问它的 BindingContext 以获取您正在寻找的视图模型属性。

      例如你的绑定声明:

      IsVisible="{Binding BindingContext.CardLoc, Source={x:Reference this} ,Converter={StaticResource MenuItemToIsVisibleReverseConverter} , ConverterParameter={x:Reference SomeElementOnPage} "
      

      在您的转换器中:

      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          var viewModel = ((SomeElementOnPage)parameter).BindingContext as YourViewModel;
      }
      

      【讨论】:

        猜你喜欢
        • 2010-12-25
        • 2013-05-29
        • 2012-04-23
        • 2011-04-28
        • 1970-01-01
        • 2012-07-04
        • 2018-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多