PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 12, 2022

[FIXED] How to write Iconverter for multiple converts

 September 12, 2022     c#, cross-platform, xamarin     No comments   

Issue

Below is the code which im using to convert the values before binding to the listview. But here only the first 2 converts are working, the results for convert3 and convert4 are not getting displayed.please help me

 <ContentPage.Resources>
        <local:Class1 x:Key="_converter"/>
    </ContentPage.Resources>

    <ContentPage.Content>
        <ListView x:Name="Models">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label  Text="{Binding from_time,Converter={StaticResource _converter}}"/>
                            <Label  Text="{Binding to_time,Converter={StaticResource _converter}}"/>
                            <Label Text="{Binding from_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>
                            <Label Text="{Binding to_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>

                        </StackLayout>
                        
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>



public class Class1: IValueConverter
    {
        public  object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var str = (string)value; // value is the binding data
            if (str== "00:00:00.0000000")
                return "";
            return value;
        }
       
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Solution

Since it works on the first and second label , I think the issue is caused by the layout . In your case , the StackLayout will not aspect the size of its child elements in runtime .

You could use Grid .

<Grid>
        <Grid.RowDefinitions>

            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>

        <Label Grid.Row="0" HeightRequest="30"  Text="{Binding from_time,Converter={StaticResource _converter}}"/>
        <Label Grid.Row="1" HeightRequest="30"  Text="{Binding to_time,Converter={StaticResource _converter}}"/>
        <Label Grid.Row="2" HeightRequest="30" Text="{Binding from_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>
        <Label Grid.Row="3" HeightRequest="30" Text="{Binding to_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>

</Grid>


Answered By - Lucas Zhang
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing