Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 187167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:46:33+00:00 2026-05-11T15:46:33+00:00

lets begin with the scenario: I have an ItemsControl inside a UserControl. In this

  • 0

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'> <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'>     <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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:46:33+00:00Added an answer on May 11, 2026 at 3:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets Say i have a table like this WEB_LIST_TABLE KEY Value ---------------------------------------- 134 google.com
Lets suppose I have a RESTful service which has a list of tasks. GET
Lets say if I have a vector V, which has 10 elements. If I
I have a class of mine, lets call it TMyObject, which should return a
Here's my scenario: Let's say I have a stored procedure in which I need
Lets say this is the situation: [Stored Proc 1] BEGIN BEGIN TRANSACTION ... exec
Lets say we have this text The 85 kilos guy rant 10 miles and
Lets say I have a stored procedure which has a simple IF block. If
Is there any way that you can combine predicates? Lets say I have something
Lets say I have these lines of code; std::vector<int> ints; std::for_each(ints.begin(), ints.end(), [](int& val){

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.