0
votes

I'm using Xamarin Forms using an MVVM approach and it works well.

I need a add functionality to an existing ListView to display text when the bound value is a certain value.

Here is the cell that generates the description

   <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding Description}"  x:Name="lblDescription" 
               Style="{DynamicResource ListItemTextStyle}" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>

Is there any way of basically only displaying the label is the following function is true (this could be in the view model or code behind) for example:

private bool IsDescriptionOk(string description)
    {
       //Look up description possibly in another lookup list
    }

id want to bind the visible to the method like:

Visible="{Binding IsDescriptionOk}"

But as its in the List View, id need to pass in the item index if that makes sense?

2
Why would you need to pass an index? Why couldn't you just return a bool based on the length of the Description property?Jason
This is just an example, i would want to look up the actual description, for example: if description was "Online", then return false, if description was "In Process" then return true etc....the_tr00per

2 Answers

1
votes

I guess there could be multiple ways of solving this kind of issues. One of them could be to use IValueConverter. Something like this:

<ViewCell>
    <StackLayout>
        <Label
            Text="{Binding Description}"
            Style="{DynamicResource ListItemTextStyle}"
            IsVisible="{Binding Description, Converter="{StaticResource ItemStatusToVisiblityConverter}" />
    </StackLayout>
</ViewCell>

However, I think that the List should contain only visible items, otherwise you may have multiple empty ListItems.

0
votes
private string _description = null;

public string Description {
  get {
    if (_description == null) {
      // method to fetch/build description 
      _description = GetDescription();
    }
    return _description;
  }

public bool IsDescriptionOK {
  get {
    if ((Description == "Online") || (other values here...)) return false;
    return true;
  }
}