Click event handler in dynamic data Template in Grid view does not work correctly

5 posts, 0 answers
  1. Nalini
    Nalini avatar
    9 posts
    Member since:
    Apr 2015

    Posted 24 Apr 2015 Link to this post

    Hello,

     I am working on directing to new page in wizard on click of hyperlink in dynamic gird view data template.

    I got some nice input specially adding events with XamlReader.Load() from http://www.telerik.com/forums/adding-click-event-handler-to-template link. Thank you for that!

    Now, the problem is that my gird view  (attached image) has "Add User" link which should open next page at very first time click but since there are total 5 links (dynamic-can be less or more)  in first row , HyperlinkButtonClick() gets called 5 times and then it open new Add User page. 

     

    Here is the code:

     

     string userDetailsTemplate =
                            "<DataTemplate xmlns='http://schemas.microsoft.com/client/2007'>" +
                                "<Grid HorizontalAlignment='Stretch'>" +
                                    "<Grid.ColumnDefinitions>" +
                                        "<ColumnDefinition />" +
                                        "<ColumnDefinition />" +
                                    "</Grid.ColumnDefinitions>" +
                                "<Grid.RowDefinitions>" +
                                    "<RowDefinition />" +
                                    "<RowDefinition />" +
                                    "<RowDefinition />" +
                                    "<RowDefinition />" +
                                "</Grid.RowDefinitions>" +
                                        "<TextBlock Text='{Binding [Username], Source={StaticResource Labels}}' Visibility='{Binding " + entp + ".Username, Converter={StaticResource VisibilityConverter}}'/>" +
                                        "<TextBlock Text='{Binding " + entp + ".Username}' Grid.Column='1' />" +
                                        "<TextBlock Text='{Binding [Phone], Source={StaticResource Labels}}' Grid.Row='1' Visibility='{Binding " + entp + ".Phone, Converter={StaticResource VisibilityConverter}}'/>" +
                                        "<TextBlock Text='{Binding " + entp + ".Phone}' Grid.Column='1'  Grid.Row='1' />" +
                                        "<TextBlock Text='{Binding [Email], Source={StaticResource Labels}}'  Grid.Row='2' Visibility='{Binding " + entp + ".Email, Converter={StaticResource VisibilityConverter}}'/>" +
                                        "<TextBlock Text='{Binding " + entp + ".Email}' Grid.Column='1'  Grid.Row='2' />" +
                                        "<HyperlinkButton Name='HLButton' Content='Add User' Visibility='{Binding " + entp + ".Username, Converter={StaticResource InvisibilityConverter}}'  Grid.Row='3'/>" +
                               "</Grid>" +
                         "</DataTemplate>";

                        DataTemplate newDT = (DataTemplate)XamlReader.Load(userDetailsTemplate);
                        externalidCol.CellTemplate = newDT;
                        this.staffGridView.AddHandler(HyperlinkButton.MouseLeftButtonUpEvent, new MouseButtonEventHandler(HyperlinkButtonClick), true);
                        this.staffGridView.Columns.Add(externalidCol);

     

     

     public void HyperlinkButtonClick(object sender, MouseButtonEventArgs e)
            {
                if (sender == null)
                    return;
                    var senderElement = e.OriginalSource as FrameworkElement;
                    var row = senderElement.ParentOfType<GridViewRow>();
                    XeRecord rec = row.Item as XeRecord;
                    selectStaff(rec);
            }

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 27 Apr 2015 Link to this post

    Hello,

    Have you subscribed for the handler 5 times? If so, then I would suggest you to subscribe for it just 1 time and then add the five column you define.

    Let me know how this works for you.

    Regards,
    Dimitrina
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. DevCraft banner
  4. Nalini
    Nalini avatar
    9 posts
    Member since:
    Apr 2015

    Posted 27 Apr 2015 in reply to Dimitrina Link to this post

    I see, below line need to be outside of loop..

     this.staffGridView.AddHandler(HyperlinkButton.MouseLeftButtonUpEvent, new MouseButtonEventHandler(HyperlinkButtonClick), true);

     

    Thanks,

    Nalini

  5. Nalini
    Nalini avatar
    9 posts
    Member since:
    Apr 2015

    Posted 28 May 2015 Link to this post

    Hello Dimitrina,

     Below line calls function HyperlinkButtonClick even on click of column header or while re-sizing the column width . I need to call it only when user will click on hyperlink of data template.

     

     this.staffGridView.AddHandler(HyperlinkButton.MouseLeftButtonUpEvent, new MouseButtonEventHandler(HyperlinkButtonClick), true);

  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 29 May 2015 Link to this post

    Hello Nalini,

    You can handle the situation based on the type of e.OriginalSource.
    For example:
    this.AddHandler(HyperlinkButton.MouseLeftButtonDownEvent, new MouseButtonEventHandler(HyperlinkButtonClick), true);
    ...
    private void HyperlinkButtonClick(object sender, MouseButtonEventArgs e)
    {
        if (e.OriginalSource.GetType() == typeof(Border))
        {
            e.Handled = true;
        }
    }

    Regards,
    Dimitrina
    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
DevCraft banner