Select to view content in your preferred language

Problem dynamically adding a click event

1866
3
01-13-2011 03:29 AM
PaulHuppé
Deactivated User
Hi,

I am trying to add an event handler to a button that is displayed dynamically in the rows of a result set from a query task.  In the results, for one of the fields, I want to display a button and add an event handler for it when the user clicks the button.

The following code without a click event handler works, but does nothing since there is no click handler:
            string attributeName = "GEOSCANREC";
            DataTemplate cellTemplate =
                    (DataTemplate)System.Windows.Markup.XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007""> 
                        <Button Content=""{Binding Attributes[" + attributeName + @"], StringFormat='\{0\}'}"" 
                        ToolTipService.ToolTip=""{StaticResource strDisplayGeoscanRecord}"" >
                        </Button> </DataTemplate>");
            QueryDetailsDataGrid.Columns.Add(new DataGridTemplateColumn()
            {
                Width = new DataGridLength(115),
                Header = LanguageResources.strGeoscanHeader.ToString(),
                CellTemplate = cellTemplate
            });

The above creates a column as seen in the image Results.png attached.

If I take the same code and add a handler:
            string attributeName = "GEOSCANREC";
            DataTemplate cellTemplate =
                    (DataTemplate)System.Windows.Markup.XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007""> 
                        <Button Content=""{Binding Attributes[" + attributeName + @"], StringFormat='\{0\}'}"" 
                            Click=""btnGeoscan_Click""
                            ToolTipService.ToolTip=""{StaticResource strDisplayGeoscanRecord}"" >
                        </Button> </DataTemplate>");
            QueryDetailsDataGrid.Columns.Add(new DataGridTemplateColumn()
            {
                Width = new DataGridLength(115),
                Header = LanguageResources.strGeoscanHeader.ToString(),
                CellTemplate = cellTemplate
            });


I get the error you see in the attached image Error.png.  This message gives you no help on what is wrong with the code.  Why can't I add the handler???

Paul
0 Kudos
3 Replies
by Anonymous User
Not applicable
Original User: danwallie

I think that since you're specifying the datatemplate in code, the XAML parser is choking on the event handler because it doesn't have the context to place it in and can't locate it. You may want to consider doing this with ICommand instead.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I agree with Dan, the XamlReader doesn't have the context to understand where to wire your click event handler up.

Another option is you to develop your own control managing the click event.
0 Kudos
by Anonymous User
Not applicable
Original User: phuppe

Hi,

Ok, thanks for the pointing me in the right direction.

Paul
0 Kudos