When I write markup I often need an IValueConverter for a Binding and usually the code implementation is small and easy. So, to resolve this typical need I thought that should be useful include small c# expression into the markup.
So I developed a MarkupExtension, called ExpressionExtension that evaluate expression during markup analyzes in order to return value to the target property. To parse the expression I used the DynamicLinq sample of SDK 3.5 which contains an useful DynamicExpression.ParseLambda method to obtain a LambdaExpression from a string. Moreover, evaluating the target property, if this is a IValueConverter, I automatically create a class, implementing the interface, that takes the delegate to convert value for a Binding. This code is compiled so is as fast as a normal c# code.
This is a partial code to understand the functionality:
// Normal expression to evaluate immediately
LambdaExpression le = DynamicExpression.ParseLambda<ExpressionDelegate>(
new ParameterExpression[] {
Expression.Parameter(typeof(object), "target")
},
typeof(object),
this.ConvertExpression,
null);
ExpressionDelegate converter = (ExpressionDelegate)le.Compile();
return converter(provideValueTarget.TargetProperty);
Here how you can use this new MarkupExtension:
<TextBox x:Name="txt" Text="Testo" />
<TextBlock
Text="{Binding Path=Text,ElementName=txt,
Converter={m:Expression value.ToString().ToUpper(culture)}}" />
<TextBlock Text="{m:Expression DateTime.Now.ToShortDateString()}" />
Here you can find the sample and source code.
In XAML 2009 there's a special syntax that will allows to use expression: Text="[DateTime.Now.ToShortDateString()]"
Due many requests for StylesExplorer code I just publishes my project on CodePlex:
http://www.codeplex.com/stylesexplorer
It contains both library for baml decompilation and the tool.
However Rob at pdc spoke about XAML 2009 and there are many new features such class to load baml/xaml so I thinks in the future to replace my library by System.Xaml.dll and work only on the tool.
When I develop custom control for WPF that supports theming I need to force the engine to load the right resource instead of my Windows Vista theme. There are some tricks on the web that does is merging the the resources for a specific theme but this works only for default WPF controls.
Here you are a snippet to force a specific theme. It uses reflection so use it only for development purpose:
public App()
{
//ForceTheme("Luna", "NormalColor");
//ForceTheme("Luna", "Homestead");
//ForceTheme("Luna", "Metallic");
//ForceTheme("Aero", "NormalColor");
//ForceTheme("Generic", "");
}
private static void ForceTheme(string themeName, string themeColor)
{
// To force a particular style
Type t = Type.GetType("MS.Win32.UxThemeWrapper, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
FieldInfo themeNameField = t.GetField("_themeName", BindingFlags.NonPublic | BindingFlags.Static);
themeNameField.SetValue(null, themeName);
FieldInfo themeColorField = t.GetField("_themeColor", BindingFlags.NonPublic | BindingFlags.Static);
themeColorField.SetValue(null, themeColor);
}
Enjoy! :-)
Since the summer I have studied how XAML markup is "compiled" to produce BAML stream included as resource into the .NET assemblies. So, during free time, I have developed an XmlReader implementation that allow to decompile BAML stream and read it as an XML.
I have also developed a tool called Styles Explorer allowing to open an application or an assembly. For each file you can see the resources and decompile them, or you can explore ResourceDictionary and preview its children.
Here a video showing its usage:

Styles Explorer
You can install and try it through ClickOnce (.NET Framework 3.5 required). Hope this is useful to you and I will try to improve and maintain Styles Expolorer, so please send to me requests and bugs.
In the future I will explain how BAML is structured.
Yes, I know, there already is a TreeListView control in the SDK, but it's simple and it isn't complete, so I developed a my custom control working on .NET Fx 3.0/3.5. It supports template, binding, scrolling and themes (classic, aero, luna). It's quite simple to use:
<
r:TreeListView ItemsSource="{Binding Source={StaticResource data},XPath=/opml/body/outline/outline}" ItemTemplate="{StaticResource dt}">
<r:TreeListView.Columns>
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding XPath=@title}" Width="150" />
<GridViewColumn Header="Childs" DisplayMemberBinding="{Binding XPath=@count}" Width="100" />
<GridViewColumn Header="Url" DisplayMemberBinding="{Binding XPath=@xmlUrl}" Width="200" />
</r:TreeListView.Columns>
</r:TreeListView>
And below a screenshot of the control in action:

The control inherits from TreeView and TreeViewItem classes and uses a custom converter to indent items. The most of work is into the xaml files, built copying markup from original TreeView and ListView styles.
I used this control into my project PaperBoy.
I hope you find useful :-). Download full source control.
I like WPF very much and I often prepare some controls for my applications. This time I want to show a simple control extension added to ListBox. Its name is AdvancedListBox that uses custom ListBoxItem and it exposes some properties to know if ListBox is loosing or capturing the selection.
Using style I have created a sample that simulates Zune navigation effect: a background animation from previous to next selection. Here the result:

WPF AdvancedListBox
And here the source.
Hi! this is my first post and I want to begin to tell you about my experiences and experiments regard .NET world.
There is a feature that lots of applications and sites miss. I'm talking about the audio spectrum, sometime built by fake bars that don't follow the music. So I developed a mixed assembly using C++ CLI that process audio samples and computes peaks grouping them by frequency range. Through DirectShow I load audio track and I grab 44100 samples per second and I transform them with FFT (I found this good guide) . FFT is heavy calculation to repeat many times per second, thus I chosen C++ CLI to reduce interop, limiting it only to the peaks exposed as a property.
The WPF side contains a custom element named AudioVisualization that 12 times per second loads peaks and shows bars using animations. My sample player contains a simple skin, but obviuosly we can customize bars exploiting WPF capabilities.
Here a video demo:

Reyalp
Full source code is available here.