*Note - Class Definition and sample data used in this example are provided in this previous blog post. Template Selectors allow you to switch the Data Template used on an item being bound based on some logic. For instance, in a banking application, you may wish an account that has a negative balance to be highlighted with a red background in order to draw the attention of the user. Other positive balance accounts can be rendered using a different, milder looking template. To implement a Template Selector, use inheritance through extending the System.Windows.Controls.DataTemplateSelector and overriding the SelectTemplate method. The signature of the method is as follows: public override System.Windows.DataTemplate SelectTemplate( object item, System.Windows.DependencyObject container) In this case, item is the actual object being bound. You can analyze the properties of the object to determine which template to use when rendering. In this ...
*Note - Class Definition and sample data used in this example are provided in this previous blog post. It's a known fact that users like to have options. Sometimes one user is more familiar with the data being displayed than another, and would to see only a summary of the data. New users of the application may prefer to see a more detailed view of the data. WPF makes it easy to define multiple Data Templates and switch them out based on user preference. To do this, you can define Data Templates as resources, and reference them by key to use them. These data templates are defined in Window.Resources: < DataTemplate x:Key ="DetailedTemplate" > < Border BorderBrush ="Blue" Margin ="3" Padding ="3" BorderThickness ="2" CornerRadius ="5" Background ="Beige" > < StackPanel Orientation ="Horizontal" > < Image ...
Sometimes the value you want to display needs to be transformed from the original data before being bound to a XAML element. For instance, formatting a telephone number, or adding brackets to a negative balance on an account. To accomplish this task a Value Converter is used. A Value Converter is simply a class that implements the IValueConverter interface (located in the System.Windows.Data namespace). This interface contains two methods, Convert, and ConvertBack. It is not necessary to implement the ConvertBack method unless you are persisting data back to a data source and need the reverse transformation from the visual back to the persisted value. It is important to remember that Value Converters can take in any value and return any type of object, for instance, an integer can be transformed into a string, and a string can be turned into a user control. In the example provided below, an integer value is converted to a User Control that has the ability to display a 5 star ...
Look Ma! WPF can look like Windows Forms too! WPF contains many similar controls that you will find in your Windows Forms toolbox. If you really wanted your app to look and feel like Windows Forms, it is quite possible... but why? There are other tools and techniques to provide users (and developers for that matter) with a much richer experience. Use the best tools for the job, and for Rich Internet or Desktop applications today the answer is WPF. Over the years, many great applications were developed using tools that were available in the day of it's creation. Cavemen used charred sticks and clay to display great masterpieces on cave walls in order to tell their story, which is in essence a way to display the data they wished to preserve. For the tools they had at their disposal at this time, these displays of data are impressive. The same analogy goes for Windows Forms, I am the first to agree that there are a number of very impressive forms applications out there, for ...