-
Lambda Expression into XAML markup
-
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()]"
-
StylesExplorer on CodePlex
-
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.
-
(Real) Force theme on WPF
-
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! :-)