tags:

views:

94

answers:

2

Is it possible to use all WPF functionality in C# (VB, ...)? Or is there any maybe advanced functionality that is not directly accessible from C#? (Maybe because it might not be accessible by public methods but rather internal.)

+6  A: 

Nothing. In fact, the actual parsing and processing (assignment of values to properties, binding to events, ...) of XAML files are eventually done by imperative C# code.

It can, however, be more tedious to set object properties using C#/VB directly; UI designers may know XAML but not C#/VB; and XML is perhaps more toolable than C# source. These factors make XAML a useful technology.

Mehrdad Afshari
I have read in earlier version of MSDN in a documentation of some feature that it is not available in C#. I don't remember where it was. I think some binding or template... Yes, XAML is XML based but it seems to me that is more difficult to debug. Sometimes it is throwing strange exceptions on strange lines: http://stackoverflow.com/questions/3277375/is-there-any-semantical-difference-between-element-property-syntax-and-atrribute (Or, maybe I am just not qualified enough to use that:)
TN
the XAML file is not compiled into code, but instead linked into the assembly or package and interpreted at runtime by System.Windows.Application.LoadComponent. The .g files are partial class definitions for member variables that are created from the "name" attribute of the XAML elements.
Ozan
@Ozan +1 (I am using `XamlReader.Parse()` and it is not invoking C#/VB compiler.
TN
Although this answer has many upvotes, it is not true.
TN
Yes, you are right that XAML is not compiled to C# (sorry if that's interpreted from my too much simplified answer) with CodeDOM like ASPX files in ASP.NET environment (although it's theoretically possible to transform XAML to pure C# code too). While you can choose to load raw XAML file at runtime, XAML files in VS are *not* embedded directly in the assembly by default. They're compiled down to BAML files and those are embedded in assembly. BAML file is loaded at runtime and essentially binds some properties of objects to some values...
Mehrdad Afshari
... Regardless of the exact way XAML is loaded, it won't change the answer to the question: essentially whatever can be done in XAML can be done in C#. C# code is doing the actual property-value bindings in the first place.
Mehrdad Afshari
+1  A: 

I think some design-time-only features like d:DataContext, d:DesignWidth and d:DesignHeight are only available in XAML, but that's about it. You can do everything else in C#/VB.NET

Thomas Levesque