Arie
Arie

Reputation: 3563

Change binded value in listview

In my listview one of the controls is label which is binded to BoolValue comming from viewmodel, in this case BoolValue is bool type so value could be either true or false. Is there any way to replace it with other text? I use MVVM.

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

Upvotes: 0

Views: 30

Answers (1)

Jason
Jason

Reputation: 89102

Yes, use a ValueConverter

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

}

then in your 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}}" />

    ...
  

Upvotes: 1

Related Questions