Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to assign an event handler for DataTemplate in code?

$
0
0

I try to dynamically create and assign a DataTemplate to a HeaderTemplate of DataGrid through code. To do this I have a method GetDatatemplate(string fromstring) that defines an XML literal and then uses this to create a DataTemplate. This works fine as long as I don't include the MouseLeftButtonDown event handler in the DataTemplate.

My DataTemplate saved in string variable StringHeaderTemplate:

private string StringHeaderTemplate =@"<DataTemplate><DataTemplate.Resources><ControlTemplate x:Key=""imgNo"" TargetType=""{x:Type Control}""><Image Source = ""pack://application:,,,/Images/upArrow.png"" /></ControlTemplate ><ControlTemplate x:Key=""imgUp"" TargetType=""{x:Type Control}""><Image Source = ""pack://application:,,,/Images/upArrow.png"" /></ControlTemplate ><ControlTemplate x:Key=""imgDown"" TargetType=""{x:Type Control}"" ><Image Source = ""pack://application:,,,/Images/downArrow.png"" /></ControlTemplate ></DataTemplate.Resources><Grid Background=""Transparent"" MouseLeftButtonDown=""Grid_MouseLeftButtonDown""><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Button Content=""Hello""/><TextBlock Grid.Row=""1"" HorizontalAlignment= ""Center"" Text = ""TextBlock"" /><CheckBox Grid.Row= ""2"" HorizontalAlignment= ""Center"" IsChecked= ""True"" /></Grid ></DataTemplate>";

And the method which gets DataTemplate:

private DataTemplate GetDatatemplate(string fromstring)
{
    ParserContext context = new ParserContext();
    context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
    context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
    return  (DataTemplate)XamlReader.Parse(fromstring, context);
}

Then I just apply this DataTemplate to HeaderTemplate of DataGrid:

DataTemplate dtCell = null;
DataTemplate dtHeader = null;
string dtString = string.Empty;
string dtHeaderString = string.Empty;
switch(Type.GetTypeCode(e.PropertyType))
{
   case TypeCode.String:
   dtString = StringTemplate.Replace("xxColumnxx", e.PropertyName);
   dtHeaderString=StringHeaderTemplate;
   break;
}
if(!string.IsNullOrEmpty(dtString))
{
   dtCell = GetDataTemplateForDataGrid(dtCellString);
   dtHeader = GetDataTemplateForDataGrid(dtHeaderString);
   DataGridTemplateColumn c = new DataGridTemplateColumn()
   {
      CellTemplate = dtCell,
      HeaderTemplate = dtHeader,
   };
   e.Column = c;
}

The event handler is really simple:

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
   MessageBox.Show(DateTime.Now.ToString());
}

The exception I get is a XamlParseException that has an InnerException of type ArgumentException that says:

"Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type."

Any ideas what to do?







Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>