Essential WPF

Browse by Tags

All Tags » Tips & Tricks (RSS)
<howto> Start Animation on Model property changed </howto>
Lately I’m teaching UI designers to work with WPF, and one of my students asked me how to start an animation when model’s property changes, and this was my answer: If the animated element is part of a DataTemplate, use DataTrigger to monitor data changes and to start the animation. But if you don’t have a DataTemplate or the animated element is not part of the DataTemplate, create a Style for that element, and use a simple DataTrigger within. < Style x : Key ="PathStyle" TargetType ...
<howto>Know that you're in design time mode</howto>
When you write markup extensions, or any other control that may work differently at runtime then design time, you may want to check if you’re in design time to pick the correct logic. In WPF, you can call the DesignerProperties.GetIsInDesignMode   attached property. In Silverlight, you may use the HtmlPage.IsEnabled property. This will work from both Blend and Cider designers. Example: if ( DesignerProperties .GetIsInDesignMode(textBox) {    return "In Design Time Mode" ;...
WPF DataGrid Search and Highlight
In this post I would like to share with you a simple solution for searching and highlighting text in the WPFToolkit or WPF 4.0 DataGrid. The Problem Lets say for example that you have a DataGrid with several columns that may display text. Now you want to be able to search terms in these text parts. For example: Proposed Solution 1. Create an attached property of type string called SearchTerm for attaching the data-grid with the search term provided by a TextBox (see images). < TextBox x : Name...
WPF BindingEx – Runtime resolved Path
One of my blog readers sent me an email regards how to create Binding to an object via XAML, where Path is unknown at design time. For example, you want to bind to Source.Property property, where Property is provided by another property. public partial class Window1 : Window { public Window1() { Path = "Name" ; Person = new Person () { Name = "Tomer Shamam" }; DataContext = this ; InitializeComponent(); } public string Path { get ; private set ; } public Person Person { get ;...
<howto>Add decorations to WPF shapes</howto>
Introduction : You have a collection of shapes (or other elements), and you want to decorate each shape with one or more decoration, such as text. Solution Actually, there are several solutions. Some are straight forward, and some are more complex but flexible. I chose to describe a flexible way in which you don’t have to touch your visual tree directly from XAML. You just need to add decorations programmatically, whenever you liked to. < Canvas x : Name ="_shapes"> < Rectangle...
<howto>Replace ListView columns with rows</howto>
Introduction : You want to present a table with a ListView , but you should display columns as rows. Problem : The only out-of-the-box view for the WPF ListView control is GridView. GridView was designed to work with columns. It displays headers and rows as two parts, using a stack layout by default. Changing the stack orientation to horizontal will not work since both the headers presenter and rows presenter are displaying children horizontally. Solution #1 : Do not use ListView. Use a ListBox with...
<howto>Create an Expander Group without Code</howto>
Introduction : You have a collection of items and you want to display them as an expander group. Only one expander should be opened at a time. Problem : There is no expander group in WPF such as panel for RadioButton , also you don't want to write custom code, only XAML. Solution : Bind the collection of items to a ListBox . Create a DataTemplate to display each item as an expander. Bind the expander IsExpanded property to the ListBoxItem.IsSelected . Override the default ListBoxItem style so...
WPF Panel & Data Binding
WPF Data Binding on behalf of ItemsControl provides an elegant solution for collection of data types. All you have to do is simple as: < ListBox ItemsSource ="{ Binding Path =Items}" ... /> But what if you want to bind the same collection to a Panel, let say UniformGrid , WrapPanel or other custom panel? Trying to bind the Panel.Children collection using the same technique above, results in a big ERROR! And the reason is: Panel.Children is not designed to be bind, hence Children is...
Data Binding for Pure CLR Objects
WPF has a great support for handling data: Data Binding. Unfortunately, one of the elements you bind to (the target) must have a DependencyProperty (known as the target-property). What if you don't have a dependency property to bind to, or you just want to bind two CLR types? For example let say that you wrote an application for simulating a car. You may have a Car and a Speedometer classes. Let say that a car handles its own speed, hence the car has its own speed state exposed by a Speed property...
WPF ComboBox and C# Enum - The Love Story
Q: How can I bind a property of type Enum (of any kind) to a ComboBox or ListBox controls? I bet that you Googled this question more than once. Did you find the correct answer? Well, I saw a lot of them, with code or XAML only, sorted, filtered, grouped, etc. With all the respect, I didn't get a generic answer with a two-way data-flow to a single enum-type property . See these links as an example: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2430540&SiteID=1 http://joshsmithonwpf...
WPF Image Processing Under the Hood
In the last two posts I have showed how to create a BitmapSource image and access its underlying WIC image, for manipulating its pixels. In this post I will explain how does the WICBitmap* works. Download the code from here . Looking inside the BitmapSource class using Reflector , it figures out that it encapsulates a WIC image (_ wicSource ). The WICBitmap class uses reflection to retrieve this private field: public WICBitmap( BitmapSource source) { Type bitmapSource = typeof ( BitmapSource ); FieldInfo...
WPF Raw Video
In my last post I showed how to access the WPF's BitmapSource underlying WIC image, and I promised to explain my code, and also to post an Image stream (Video) code based on my solution. In this post I will quickly talk about the unique CompositionTarget class, and I will show how to render a Raw Video using its Rendered event. Download the code from here ( FYI - I refractor the code from the last post and fixed several bugs ). I will start by reminding one of the disadvantages of my WICBitmap...
WPF Image Processing
With the release of Windows Vista and WPF, Microsoft has also released the WIC: "an extensible framework for working with images and image metadata. WIC makes it possible for independent software vendors (ISVs) and independent hardware vendors (IHVs) to develop their own image codecs and get the same platform support as standard image formats (for example, TIFF, JPEG, PNG, GIF, BMP, and WMPhoto)" (MSDN). Unsurprisingly, WPF uses WIC in its core (for example, BitmapSource ). Unfortunately...