views:

707

answers:

1

Hi there,

lets begin with the scenario:

I have an ItemsControl inside a UserControl. In this ItemsControl I have a dynamicly created DataTemplate which is created and added in codebehind. As there doesn't seem to be a nice way to create a DataTemplate in codebehind I had to programmatically generate the xaml code for my DataTemplate into a string and then create a DataTemplate object out of it through XamlReader:

StringBuilder stringBuilder = new StringBuilder();
XmlWriter xmlWriter = XmlWriter.Create(stringBuilder);

... // use xmlWrite to generate desired xaml

// substring is use to cut out the xml declaration
DataTemplate template = (DataTemplate)XamlReader.Load(stringBuilder.ToString().Substring(39));
myItemsControl.ItemTemplate    = template;

The generated XAML code looks like this and is actually used (the items get rendered as expected):

<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&gt;
<Grid HorizontalAlignment="Stretch" Margin="0,0,0,0">
 <Grid.ColumnDefinitions>
  <ColumnDefinition Width="*" />
  <ColumnDefinition Width="100" />
  <ColumnDefinition Width="100" />
  <ColumnDefinition Width="100" />
 </Grid.ColumnDefinitions>

 <TextBlock Text="{Binding b0}" Grid.Column="0" />
 <TextBox Text="{Binding b1, Converter={StaticResource customConverter}}" HorizontalAlignment="Stretch" Grid.Column="1" LostFocus="TxtAttribute_LostFocus" />
 <TextBox Text="{Binding b2, Converter={StaticResource customConverter}}" HorizontalAlignment="Stretch" Grid.Column="2" LostFocus="TxtAttribute_LostFocus" />
 <TextBox Text="{Binding b3, Converter={StaticResource customConverter}}" HorizontalAlignment="Stretch" Grid.Column="3" LostFocus="TxtAttribute_LostFocus" IsReadOnly="True" />
</Grid>

In case you wonder: the xmlns attribute is needed by the XamlReader to render the control, else you'll get an exception when reaching the code.

My problem:

now while the items look like expected and data is correctly bound neither my customConverter that should reformat the bound data, nor the LostFocus event are correctly applied. I don't get any error messages or warnings, converter and event just don't get called. Anyone an idea why and how I can get this to work?

Update:

I reached a point where I have to solve this problem or to try a different approach. In my last tests I tried to add the Converter directly in the DataTemplate but I had no luck. The generated code now looks like this:

<DataTemplate xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Conv="clr-namespace:my.Namespace" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&gt;
    <Grid HorizontalAlignment="Stretch" Margin="0,0,0,0"> 
     <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="80" />
      <ColumnDefinition Width="80" />
      <ColumnDefinition Width="80" />
     </Grid.ColumnDefinitions>

     <Grid.Resources>
      <Conv:DecimalConverter x:Name="cnvDecimalConverter" />
     </Grid.Resources>

     <TextBlock Text="{Binding b0}" Grid.Column="0" />
     <TextBox Text="{Binding b1, Converter={StaticResource cnvItemsDecimalConverter}}" HorizontalAlignment="Stretch" Grid.Column="1" LostFocus="TxtAttribute_LostFocus" />
     <TextBox Text="{Binding b2, Converter={StaticResource cnvItemsDecimalConverter}}" HorizontalAlignment="Stretch" Grid.Column="2" LostFocus="TxtAttribute_LostFocus" />
     <TextBox Text="{Binding b3, Converter={StaticResource cnvItemsDecimalConverter}}" HorizontalAlignment="Stretch" Grid.Column="3" LostFocus="TxtAttribute_LostFocus" IsReadOnly="True" />
    </Grid>
</DataTemplate>

Any ideas?

Update 2:

As I just found out XamlReader.Load() just is not able to hook up events. See this Thread in the Silverlight Forums

The Converters should work, I guess I still have some kind of namespace problem I don't see. I'm kind of out of options with my "simple" ItemsControl approach so I think it's time to look for another method to reach my needs.

A: 

Just to clear up the situation: It is not possible to generate dynamic DataTemplates with events through generating an xaml string and extract the control from this. The only option to parse xaml code with events is through Application.LoadComponent which needs a URI to work.

I ended up using nested ItemControls to create my "dynamic" behaviour.

gsnerf