【发布时间】: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