Xamarin Forms converter and binding does not work

HarbHarb USMember ✭✭
edited February 2017 in Xamarin.Forms

Hello everyone!
I've seen similar questions, although I did not find the answer...

The main goal is to put the messages on the left or on the right based on the bool value.
My converter and binding does not work. The ConversationMessage objects are ok, because messageText and messageDate are bound and visible on the page for every message.
I am trying to just at least put the convert result in a label, however those are not shown also.
What do I miss?

The code is the following:

C#
//isMyMessage Converter
class BoolToPositionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (string)((bool)value ? "End" : "Start");
}

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((value as string).Equals("End")) ? true : false;
        }
    }

//class for messages
    public class ConversationMessage
    {
        public String MessageDate { get; set; }
        public String MessageText { get; set; }
        public Boolean IsMyMessage { get; set; }
        public int MessageGroupID { get; set; }
    }

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CrossApp1.Views;assembly=CrossApp1"
             x:Class="CrossApp1.Views.ConversationPage">

  <ContentPage.Resources>
    <ResourceDictionary>
      <local:BoolToPositionConverter x:Key="BoolPosConv"></local:BoolToPositionConverter>
    </ResourceDictionary>
  </ContentPage.Resources>

  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="128"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="48" />
    </Grid.RowDefinitions>
      <ListView x:Name="messagesListView" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
                VerticalOptions="FillAndExpand" SeparatorVisibility="Default">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <StackLayout>
                <Label Text="{Binding MessageDate}" HorizontalOptions="{Binding IsMyMessage, Converter={StaticResource BoolPosConv}}" />
                <Label Text="{Binding MessageText}" HorizontalOptions="{Binding IsMyMessage, Converter={StaticResource BoolPosConv}}" />
                <Label Text="{Binding IsMyMessage, Converter={StaticResource BoolPosConv}}" />
              </StackLayout>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
  </Grid>
</ContentPage>

Best Answer

Answers

  • HarbHarb USMember ✭✭
    edited February 2017

    Works!
    A lot of thanks for quick ans useful response!

    Here is the solution:

        //isMyMessage Converter
        class BoolToPositionConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                LayoutOptions positioning = LayoutOptions.Center;
                if (value != null && value.GetType() == typeof(Boolean))
                {
                    positioning = (bool)value ? LayoutOptions.End : LayoutOptions.Start;
                }
            return positioning;
            }
    
        //not in use
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return false;
            }
        }
    

    XAML section:

            <ListView.ItemTemplate>
              <DataTemplate>
                <ViewCell>
                  <StackLayout>
                    <Label Text="{Binding MessageDate}" HorizontalOptions="{Binding IsMyMessage, Converter={StaticResource BoolPosConv}}" />
                    <Label Text="{Binding MessageText}" HorizontalOptions="{Binding IsMyMessage, Converter={StaticResource BoolPosConv}}" />
                  </StackLayout>
                </ViewCell>
              </DataTemplate>
            </ListView.ItemTemplate>
    
Sign In or Register to comment.