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

  • Home
  • SEARCH
  • 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 9151797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:53:24+00:00 2026-06-17T11:53:24+00:00

I am attempting to form some xaml for a dataform programmatically using a string.

  • 0

I am attempting to form some xaml for a dataform programmatically using a string. I can get the combo box to appear. but when I attempt to use the code specifying the “MouseLeftButtonUp” or the “Loaded” event handler in the string; the page will turn white (no noticeable error) out after going into it. Please see relevant code below.

     StringBuilder editTemplate = new StringBuilder("");
     editTemplate.Append("<DataTemplate ");
     editTemplate.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
     editTemplate.Append("xmlns:toolkit='http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit' ");
     editTemplate.Append("xmlns:navigation='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation' ");
     editTemplate.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' >");
     editTemplate.Append("<StackPanel>");
     editTemplate.Append(@"  <toolkit:DataField Label='" + GetFieldWithoutNumber(theInfo, theDataContext) + "'>");
     /* Won't Work */ editTemplate.Append(@" <ComboBox MouseLeftButtonUp='ComboBox_MouseLeftButtonUp' />");
     /* Will Work  */ editTemplate.Append(@" <ComboBox />");
     editTemplate.Append(@" </toolkit:DataField>");
     editTemplate.Append("</StackPanel></DataTemplate>");
     dynamicDataForm.EditTemplate = XamlReader.Load(editTemplate.ToString()) as DataTemplate;
  • 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. Editorial Team
    Editorial Team
    2026-06-17T11:53:25+00:00Added an answer on June 17, 2026 at 11:53 am

    Event handlers hooked up in XAML are required to be declared in the code-behind connected to the XAML file. In the case of a ResourceDictionary or anything loaded from XamlReader.Load there can’t be any code-behind, so event handlers can’t be set in the XAML. The easiest way to get around this restriction would be to not build your template from strings and just declare it in the Resources section of your XAML file which you can then do like:

    Resources["MyTemplate"] as DataTemplate

    to get the template and assign it in code like you’re doing here, or just use StaticResource in XAML. As long as it stays in the same XAML file connected to this code the event handlers you have in it currently should work fine. The dynamic part of the strings would also need to be changed to use Bindings.

    If you want to stick with the XamlReader method you have 2 problems to solve.

    1. Locate the ComboBox instance inside the rendered template
    2. Wait until the template is rendered to look for the ComboBox

    To find the ComboBox you need to first give it an x:Name attribute in the template text (you can just replace the event code currently there). Next you need to be able to locate an item in the visual tree by name. This is fairly straightforward and you can find an example here to do that.

    To call this code at the right time you either need to override OnApplyTemplate, which unfortunately won’t work if you’re in something like a UserControl, or use another trick to keep it from running until all the controls are rendered. Here’s a full example that could go in a constructor and uses the find method linked from above:

    DataTemplate template = Resources["MyTemplate"] as DataTemplate;
    dynamicDataForm.ContentTemplate = template;
    
    Dispatcher.BeginInvoke(() =>
    {
        ComboBox button = FindVisualChildByName<ComboBox>(this, "MyControl");
        if (button != null)
            button.MouseLeftButtonUp += (s, _) => MessageBox.Show("Click");
    });
    

    In your case it looks like your template might need to wait to switch to an edit state before it renders in which case you’d need to hold off on connecting the event and find some other event on your dataform that happens when that state is changed.

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

Sidebar

Related Questions

I am attempting to get my arms around some basic prolog but struggling a
I am attempting to test some code that uses Request.Item(key) but I can not
I am attempting to clone fields in a form using Jquery. I'm trying to
I am attempting to have mechanize select a form from a page, but the
I am attempting to get the basic form of crawler4j running as seen here
I'm attempting to use the rest-client gem to post something, but for some reason,
I am attempting to convert some code from TWebBrowser to Chromium but am having
I'm attempting to get over the hurdle of dynamic form fields in Rails --
I am having some issues with nested models in a form, using Rails 3.1rc4.
I've been attempting to modify some code that I was using previously with jQuery

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.