1

I have a listview like the following picture. There is a more icon(in blue color) and switch on the right side of each list item.

enter image description here

I have a string variable called type, if "type" value is "switch", I need to hide the more icon from the list and show switch icon only and if the "type" value is "more" hide the switch from the list and show more icon only.

The more and switch are inside listview like below:

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout>
                        //Items like profile image name 
                        <Image   //more icon     />
                        <Switch/>
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Thanks in advance

3
  • Yo are try with a DataTemplateSelector? Commented Apr 11, 2018 at 9:12
  • Yes, In listview I am using DataTemplate. Commented Apr 11, 2018 at 9:20
  • The DataTemplateSelector groups together various DataTemplate. You could have two DataTemplate , one with the content and the other with 0 high. Commented Apr 11, 2018 at 16:14

2 Answers 2

1

You can try to create a converter:

public class StringToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((string)value == "switch")
        {
            if ((string)parameter == "Image") return false;

            return true;
        }
        if ((string)parameter == "Image") return true;
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Then use this in the page xaml:

<ContentPage.Resources>
    <ResourceDictionary>
        <local:StringToBoolConverter x:Key="stringToBool" />
    </ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
    <ListView x:Name="MyListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <StackLayout Orientation="Horizontal">
                            <Image IsVisible="{Binding Path=type, Converter={StaticResource stringToBool}, ConverterParameter=Image}" />
                            <Switch IsVisible="{Binding Path=type, Converter={StaticResource stringToBool}, ConverterParameter=Switch}"/>
                        </StackLayout>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

Convert the string to bool, and use a parameter to tell the converter which control should be hidden.

Sign up to request clarification or add additional context in comments.

4 Comments

Where is the type check happening?
@SreejithSree I misunderstood your meaning. Please see my updating.
I added your codes, but in both cases, both icons are showing.
@SreejithSree I make a simple sample for you here
0

You could create a converter (like Land Lu mentioned) or you could just create a property for each. If you're only going to do it for just a couple of items, then a property is good enough. If you have a bunch of them, then using a converter would probably be a better approach.

In your BindingContext for this page (should be the ViewModel), add two new properties:

public bool CanShowSwitch => type == "switch";
public bool CanShowMore => type == "more";

With "type" being a public string property in this same class. Now just update your xaml to add the IsVisible property:

<Image IsVisible="{Binding CanShowMore}" />
<Switch IsVisible="{Binding CanShowSwitch}"/>

Alternatively, if there is only two items, and you want to show either/or, you could have only one property and use an inverse boolean converter, but at that point, you're using a converter and might as well go with the other converter approach. All three are different ways to solve the same issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.