This is a migrated thread and some comments may be shown as answers.

RadListView item tap values

4 Answers 355 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Mauricio
Top achievements
Rank 1
Mauricio asked on 09 Jun 2017, 11:46 AM

Hi, i have a rad list view implemented and i need to retrieve the value of one of the elements inside of it, this is my hierarchy of elements:

 

<telerikDataControls:RadListView x:Name="Results" BackgroundColor="White" Grid.Row="0" ItemTapped="Results_ItemTapped">
                        
                        <telerikDataControls:RadListView.ItemTemplate>
                            <DataTemplate>
                                <telerikListView:ListViewTemplateCell>
                                    <telerikListView:ListViewTemplateCell.View>
                                        <Grid >

                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            
                                            <Image Source="{Binding Path=hier_image}"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
                                            
                                            </Image>
                                            <Label Text="{Binding Path=hier_name}"  Grid.Row="0" HorizontalOptions="End" VerticalOptions="End" />
                                            <Label Text="{Binding Path=hier_id}" IsVisible="False"/>
                                        </Grid>
                                    </telerikListView:ListViewTemplateCell.View>
                                </telerikListView:ListViewTemplateCell>
                            </DataTemplate>
                        </telerikDataControls:RadListView.ItemTemplate>
                        <telerikDataControls:RadListView.LayoutDefinition>
                            <telerikListView:ListViewGridLayout HorizontalItemSpacing="5"
                                                ItemLength="120"
                                                SpanCount="2"
                                                VerticalItemSpacing="5"/>
                        </telerikDataControls:RadListView.LayoutDefinition>
                    </telerikDataControls:RadListView>

 

I need the value of the label with text={ binding path=hier_id} of the item tapped, how can i do this? any approach?

 

Regards.

4 Answers, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 09 Jun 2017, 03:41 PM
Hello Mauricio,

You can get the actual bound item in the ItemTapEventArgs. Using your model, here's an example of the ItemTapped event handler:

private void Results_ItemTapped(object sender, ItemTapEventArgs e)
{
    // 1 - Use the args to get the Item and cast to the model
    var tappedHier = e.Item as Heir;
 
    // 2 - Now you can access any of the property values
    var name = tappedHier?.hier_name;
}


Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Desarrollo
Top achievements
Rank 1
answered on 16 May 2018, 10:24 PM

Hi Lance, i'm getting this System.InvalidCastException: Specified cast is not valid. and i'm doing the same of you did on the example

and they are the same object.

System.ArgumentException: The field '<Item>k__BackingField' is not valid for this type.
   en Mono.Debugger.Soft.ObjectMirror.GetValues(IList`1 fields) en E:\A\_work\160\s\external\debugger-libs\Mono.Debugger.Soft\Mono.Debugger.Soft\ObjectMirror.cs:línea 93
   en Mono.Debugger.Soft.ObjectMirror.GetValue(FieldInfoMirror field) en E:\A\_work\160\s\external\debugger-libs\Mono.Debugger.Soft\Mono.Debugger.Soft\ObjectMirror.cs:línea 74
   en Mono.Debugging.Soft.FieldValueReference.get_Value() en E:\A\_work\160\s\external\debugger-libs\Mono.Debugging.Soft\FieldValueReference.cs:línea 129
   en Mono.Debugging.Evaluation.NRefactoryExpressionEvaluatorVisitor.VisitAsExpression(AsExpression asExpression) en E:\A\_work\160\s\external\debugger-libs\Mono.Debugging\Mono.Debugging.Evaluation\NRefactoryExpressionEvaluatorVisitor.cs:línea 588
   en ICSharpCode.NRefactory.CSharp.AsExpression.AcceptVisitor[T](IAstVisitor`1 visitor) en E:\A\_work\160\s\external\NRefactory\ICSharpCode.NRefactory.CSharp\Ast\Expressions\AsExpression.cs:línea 69
   en Mono.Debugging.Evaluation.NRefactoryExpressionEvaluator.Evaluate(EvaluationContext ctx, String expression, Object expectedType) en E:\A\_work\160\s\external\debugger-libs\Mono.Debugging\Mono.Debugging.Evaluation\NRefactoryExpressionEvaluator.cs:línea 82
   en Mono.Debugging.Evaluation.ObjectValueAdaptor.GetExpressionValue(EvaluationContext ctx, String exp) en E:\A\_work\160\s\external\debugger-libs\Mono.Debugging\Mono.Debugging.Evaluation\ObjectValueAdaptor.cs:línea 1386

 

0
Desarrollo
Top achievements
Rank 1
answered on 17 May 2018, 03:10 PM
sorry i resolve the issue using this Telerik.XamarinForms.DataControls.ListView.ItemTapEventArgs instead of Xamarin.Forms.ItemTapEventArgs
0
Lance | Manager Technical Support
Telerik team
answered on 17 May 2018, 03:28 PM
Hi Ernesto,

I'm glad you found the problem quickly. Indeed, the ItemTapped args used for the event handler are different than the Xamarin.Forms one.

For any folks visiting this thread in the future, here's a more complete code example:

View:

<?xml version="1.0" encoding="utf-8" ?>
    xmlns:telerikDataControls="clr-namespace:Telerik.XamarinForms.DataControls;assembly=Telerik.XamarinForms.DataControls"
    xmlns:listView="clr-namespace:Telerik.XamarinForms.DataControls.ListView;assembly=Telerik.XamarinForms.DataControls"
    x:Class="TelerikXamarin.Portable.StartPage">

    <telerikDataControls:RadListView x:Name="Results" ItemTapped="Results_ItemTapped">
...
    </telerikDataControls:RadListView>
</ContentPage>


Model and code-behind:

public class Heir
{
    public string hier_image { get; set; }
    public string hier_name { get; set; }
    public string hier_id { get; set; }
}
 
public partial class StartPage : ContentPage
{
    public StartPage()
    {
        InitializeComponent();
    }
 
    private void Results_ItemTapped(object sender, Telerik.XamarinForms.DataControls.ListView.ItemTapEventArgs e)
    {
        // 1 - Use the args to get the Item and cast to the model
        var tappedHier = e.Item as Heir;
 
        // 2 - Now you can access any of the property values
        var name = tappedHier?.hier_name;
    }
}

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
ListView
Asked by
Mauricio
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Desarrollo
Top achievements
Rank 1
Share this question
or