【问题标题】:Xamarin Forms pass model value to binding converter parameterXamarin Forms 将模型值传递给绑定转换器参数
【发布时间】:2017-04-19 06:29:25
【问题描述】:

我想将模型属性值像转换器参数一样传递给其他绑定值。

<Label FontSize="{Binding MediumLabelTextSize}"
       Text="{Binding FeedHome.QuestionBody,
              Converter={StaticResource HideLongTextConverter}}"/>
<Label FontSize="{Binding SmallLabelTextSize}"
        IsVisible="{Binding IsQuestionReadMoreVisible}"
        Text="Read more">
        <Label.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding ReadMoreCommand}"
                            CommandParameter="{Binding .}"/>
        </Label.GestureRecognizers>
</Label>

我想像 IsQuestionReadMoreVisible 的转换器参数一样传递 FeedHome.QuestionBody。 我该怎么做?

【问题讨论】:

  • 从技术上讲,视图不应将某些内容发布到视图模型。 Command 和 CommandParameter 是唯一的例外。如果您的视图中有数据,Viewmodel 应该已经意识到这一点
  • IsVisible="{Binding IsQuestionReadMoreVisible, ConverterParameter={Binding Source={x:Reference LabelName}, Path=Text}}" 这也可以使用,否则您可以使用传递 BindingContext 像“{Binding BindingContext .FeedHome.QuestionBody, Source={x:Reference MainPage}}"
  • 为什么不在 viewModel 的 ReadMoreCommand 中使用 FeedHome.QuestionBody 属性?当你点击标签时,你知道这个属性的值吗?
  • 如果 FeedHome.QuestionBody 文本长度超过 300 个符号,我需要设置 IsQuestionReadMoreVisible = true,并且我认为像 IsQuestionReadMoreVisible 的转换器参数一样传递 QuestionBody。

标签: c# xamarin xamarin.forms


【解决方案1】:

请试试这个

Text="{Binding HeaderText, Converter={StaticResource caseConverter}, ConverterParameter=u}}" IsVisible="{Binding IsEditMode, Converter={StaticResource boolInverter}}"

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 2012-10-05
    • 1970-01-01
    • 2022-01-19
    • 2018-01-09
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多