RadListView item tap values

5 posts, 0 answers
  1. Mauricio
    Mauricio avatar
    2 posts
    Member since:
    Dec 2014

    Posted 09 Jun 2017 Link to this post

    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.

  2. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1168 posts

    Posted 09 Jun 2017 Link to this post

    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
  3. Desarrollo
    Desarrollo avatar
    11 posts
    Member since:
    May 2011

    Posted 16 May 2018 in reply to Lance | Manager Technical Support Link to this post

    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

     

  4. Desarrollo
    Desarrollo avatar
    11 posts
    Member since:
    May 2011

    Posted 17 May 2018 in reply to Desarrollo Link to this post

    sorry i resolve the issue using this Telerik.XamarinForms.DataControls.ListView.ItemTapEventArgs instead of Xamarin.Forms.ItemTapEventArgs
  5. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1168 posts

    Posted 17 May 2018 Link to this post

    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
Back to Top