【问题标题】:Change binded value in listview更改列表视图中的绑定值
【发布时间】:2021-04-08 09:24:40
【问题描述】:

在我的listview 中,其中一个控件是label,其中is bindedBoolValue 来自viewmodel,在这种情况下BoolValue 是布尔类型,因此值可以是true 或@987654328 @。有没有办法用其他文本替换它?我使用 MVVM。

  <Label
       Text="{Binding BoolValue}" />
  <Label

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    是的,使用ValueConverter

    public class BoolToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value != 0) ? "True Value" : "False Value";
        }
    
    }
    

    然后在你的 XAML 中

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyDemo"
             x:Class="MyDemo.MyPage"
             >
        <ContentPage.Resources>
            <ResourceDictionary>
                <local:BoolToStringConverter x:Key="BoolToString" />
            </ResourceDictionary>
        </ContentPage.Resources>
    
        ...
    
        <Label Text={Binding BoolValue, Converter={StaticResource BoolToString}}" />
    
        ...
      
    

    【讨论】:

    • 我看到的是这个转换器 BoolToStringConverter 只能转换成两个值 True Value 或 False Value。如果另一个例如标签想要有另一个反对假/真的值怎么办。是否可以根据我想要不同值的不同控件说转换器?例如在这一行:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 2015-05-24
    • 1970-01-01
    • 2011-03-02
    • 2013-08-12
    • 1970-01-01
    相关资源
    最近更新 更多