set index of binding list from another list

Hello,
i am new at xamarin i have an issue and i don't have an idea how to solve it.
assume i have two lists
public class A
{
public int Id { get; set; }
public int refIndx { get; set; }
public string SomeString { get; set; }
}

public class B
{
public string SomeString { get; set; }
.....
}

in xaml :

        <ListView x:Name="FlightList">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid Padding="5">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Frame CornerRadius="10"  
                                   OutlineColor="DarkOrange" 
                                   Padding="0">
                                <StackLayout Grid.Row="0">
                                                <Span Text="{Binding A.SomeString }" /> //this works fine
                                                <Span Text="{Binding b[A.refIndx].SomeString }" /> //nvalid property path syntax                                        
                                </StackLayout>
                            </Frame>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

do you have any idea how to achieve this?

regards

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Firstly, try to create a data source like:

    public class B
    {
        public static B Current = new B();
        public List<string> SomeString { get; set; }
        public B()
        {
            SomeString = new List<string>();
            for (int i=0; i<10; i++)
            {
                SomeString.Add("item" + i);
            }
        }
    }
    

    Then we need to create a converter like:

    public class CustomConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            A aModel = value as A;
            B bModel = parameter as B;
            if (aModel != null)
            {
                return bModel.SomeString[aModel.refIndx];
            }
            return null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Consume this converter in Xaml like:

    <ContentPage.Resources>
        <local:CustomConverter x:Key="CustomConverter"/>
    </ContentPage.Resources>
    
    <StackLayout>
        <ListView x:Name="FlightList">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid Padding="5">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Frame CornerRadius="10"  
                                    OutlineColor="DarkOrange" 
                                    Padding="0">
                                <StackLayout Grid.Row="0">
                                    <Label Text="{Binding SomeString}" />
                                    <Label Text="{Binding ., Converter={StaticResource CustomConverter}, ConverterParameter={x:Static local:B.Current}}" />
                                </StackLayout>
                            </Frame>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    

    I passed current view cell's binding context(class A) to the converter so that we could get the refIndx there. Meanwhile, I passed the static B to the converter then we could retrieve the other class through the parameter. You could refer to my attachment to see the specific code.
    However, normally we don't make it like this. Define a new property in class A and put the information we needed from the class B using code behind. At last, we could access all the properties using a single resource.

Sign In or Register to comment.