简体   繁体   中英

Adding Image/Button Click Event Handler Dynamically in C# Silverlight

Please check the following:

/// <summary>
/// This method returns a custom column control for the AgDataGrid from devex with a   button design including button click event handlers
/// </summary>
/// <param name="headerContent">The header column value for this cell</param>
/// <param name="control">The DataTemplate XAML needed to Load the control</param>
/// <param name="routedEventHandler">The Button Click Event</param>
/// <returns>AgDataGridColumn</returns>
public AgDataGridColumn CustomColumn(string headerContent, string control, RoutedEventHandler routedEventHandler)
{
AgDataGridColumn _AgDataGridColumn = new AgDataGridColumn();
_AgDataGridColumn.HeaderContent = headerContent;
_AgDataGridColumn.CellDisplayTemplate = XamlReader.Load(control) as DataTemplate;
_AgDataGridColumn.AllowSorting = DefaultBoolean.False;
_AgDataGridColumn.PrepareCellDisplayElement += (d, _e) =>
{
    ((Button)_e.DisplayElement).Click -= routedEventHandler;
    ((Button)_e.DisplayElement).Click += routedEventHandler;
};
return (_AgDataGridColumn);
}

I would like to convert this method to the silverlight datagrid. I am trying to find a proper way of doing the PrepareCellDisplayElement line, adding the event handlers. That method does not seem to exist for sl datagrid. Any help would be appreciated.

One solution can be found @ http://forums.silverlight.net/forums/p/189927/440746.aspx . Unfortunately, I do not like the idea of attaching the click event handler on every single data row load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM