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

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

4 Answers 300 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nalini
Top achievements
Rank 1
Nalini asked on 24 Apr 2015, 04:32 PM

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);
        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 27 Apr 2015, 06:48 AM
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.

 
0
Nalini
Top achievements
Rank 1
answered on 27 Apr 2015, 05:21 PM

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

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

 

Thanks,

Nalini

0
Nalini
Top achievements
Rank 1
answered on 28 May 2015, 04:00 PM

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);

0
Dimitrina
Telerik team
answered on 29 May 2015, 08:09 AM
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
Tags
GridView
Asked by
Nalini
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Nalini
Top achievements
Rank 1
Share this question
or