How can I convert this XAML code into C# code?

How can I convert this XAML code into C# code?

<Window.Resources> <DataTemplate x:Key="itemtemplate"> <TextBlock Text="{Binding Path=Text}"/> </DataTemplate> </Window.Resources>

-------------Problems Reply------------

Try the following. Not an imperative WPF expert so you may need to alter this slightly

public void Example()
var factory = new FrameworkElementFactory(typeof(TextBlock));
factory.SetBinding(TextBlock.TextProperty, new Binding("Text"));

var dataTemplate = new DataTemplate();
dataTemplate.VisualTree = factory;

The correct way to create DataTemplates from C# is to use a XamlReader and give it what you wrote in your question.

Which is unpleasant, to say the least. Sorry.

I just checked the online docs - Alun is correct - use the XamlReader. According to Microsoft, the FrameworkElementFactory class does not support all of the features of XAML, and may be deprecated in the future.

Having said that, I've used FrameworkElementFactory to alter DataTemplates on-the-fly, and didn't have any problems.

Category:c# Views:2 Time:2009-03-05
Tags: c# wpf

Related post

  • Is there any tool to convert from XAML to C# (code behind)? 2009-03-29

    I am looking for an easy way to convert some XAML animation to C# code. Is there any tool available to do that? Thanks! Wally This is the code I am trying to convert: <Storyboard x:Key="Storyboard1"> <Rotation3DAnimationUsingKeyFrames BeginT

  • Can you convert a XAML DrawingImage back into an SVG for editting? 2009-06-03

    A design company made an application design in WPF 2 years ago, and now we're looking at changing the text on one of the images. No SVG files were provided, only the XAML code used, and the developer who worked on the project at the design company no

  • Convert WPF XAML to Silverlight XAML 2009-12-10

    I have question about using XAML across the WPF and Silverlight platforms. Background: I have a silverlight app that needs to pass the Xaml to WPF and do some calculation to update the XAML. When I run the changes in WPF, I parse the XAML and convert

  • wpf converter in xaml 2010-05-28

    I know there are a few ways to use value converter in xaml, like using StaticResource to get converter from resources or Static to get from a static instance. My question is, for whatever reason, I have a converter instance created in the code behind

  • Is there a way to programmatically convert other Xaml elements to path elements? 2010-09-08

    In Blend, it is possible to convert certain Xaml elements to Path, using Object->Path->Convert to Path option. Is there some API to do the same programmatically, in a WPF application? Thanks --------------Solutions------------- Having dived int

  • Is there an effective tool to convert C# code to Java code? 2008-09-17

    Is there an effective tool to convert C# code to Java code? --------------Solutions------------- I have never encountered a C#->Java conversion tool. The syntax would be easy enough, but the frameworks are dramatically different. Even if there wer

  • How to convert C# code to object code? 2009-04-28

    Hai guys, Can any help me how to convert c# code to compiler code or object code --------------Solutions------------- If you mean MSIL (.NET bytecode) the easiest way to convert C# into that is to... compile it? Pressing build in Visual Studio ought

  • Convert a single file aspx to code behind 2009-05-12

    I'm working on a web site (not a web application) in VS 2008 .Net 3.5 and it uses the single file .aspx model where the server code is included in the head portion of the html instead of using a .aspx.cs code behind page. I'd like to quickly convert

  • Convert a .jar file into a .cod file using bb-ant 2010-06-22

    I'm working on a Blackberry application (JDE 4.6.1) on a Windows system. I need to convert a .jar file in a .cod file using bb-ant-tools. Does anybody know how to do it? I'm using: <target name="convert"> <rapc import="source.jar" destdir="$

  • How Can i convert a perl script into C code? 2010-07-25

    How can I convert a perl script into C code? --------------Solutions------------- You rewrite it, there's no effective way to do it otherwise. Start to learn about C (and Perl if you have already not), then translate them manually. (Or hire a program

  • How to convert .bat code to .php code 2010-11-09

    I want use webscreencapture to make a web screenshot. if I save the below code in a screenshot.bat, and then click it, it can catch the two web screenshots. webscreencapture.exe e:\www\111\1.jpg webscreencap

  • convert matlab code to c code 2011-01-18

    is there s a way to convert simulink blocks or a matlab ".m" code to C code automatically ? --------------Solutions------------- To convert Simulink models or MATLAB m-code to C you need Real-Time Workshop. It supports only subset of MATLAB language

  • How to convert machine code to assembly code? 2011-04-12

    Please suggest me How to convert machine code to assembly code? excluding intel reference manual and dos debugger method? --------------Solutions------------- You can use any debugger, such as gdb, or any disassembler, such as IDA Pro Advanced. There

  • Are there any utiltiies to convert a visio flowchart to Java code? 2011-04-18

    Im looking to streamline a development process, where most data flows are represented as flowcharts (end users are not developers). Is there any utility out there that can convert a visio flowchart to java code? Im thinking a simple framework that co

  • Convert a xaml file to an bitmap 2011-06-20

    is it possible to convert a xaml-file to an image file of some sort? I have some xaml-files describing a GUI, and a want to get a 'preview' of them at runtime. Greets, Jürgen --------------Solutions------------- maybe the RenderTargetBitmap can help

  • how is convert and fix error in this code to CodeIgniter? 2011-07-14

    how is convert this code to CodeIgniter code: search_hotel: -> this is CI_Model return mysql_query("select * from hotel_submits where name LIKE '".$searchterm."'") i try but have error: $query = $this->db->order_by("id", "desc")->like('na

  • Possible to convert vbs code to javascript code 2011-07-20

    i want to Convert vbs code to javascript code in application so is this possible? --------------Solutions------------- Learn JavaScript, convert your existing VBS code to JavaScript: Best resources to learn JavaScript

  • How to convert signal name (string) to signal code? 2011-08-09

    I am writing a program that reads the name of the signal (e.g. SIGSTOP, SIGKILL etc) as a string from the command line and calls the kill() system call to send the signal. I was wondering if there is a simple way to convert the string to signal codes

  • Need help converting python code to c code 2011-08-09

    Is there any compiler which will help me convert my python code to proper C code? In my python code I am using the xml.dom.minidom module to parse an xml and process the results obtained by ElementsByTagName. I don't know of any such compiler which w

Copyright (C), All Rights Reserved.

processed in 0.181 (s). 11 q(s)