


Here's the source code of the sample project. With a DataTemplateSelector you're not restricted to dependency properties. Then use an instance as ItemTemplateSelector: String template = XamlReader.Load(template) as DataTemplate Īgain, register the class contining the logic as a resource: String backGroundColor = converter.Convert((item as Beer).Color, null, null, null).ToString() If (!(("Belgian") || ("Flemish")))īeerColorConverter converter = new BeerColorConverter() Protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) Public class BeerTemplateSelector : DataTemplateSelector The following chauvinist logic creates a colored stackpanel including a Belgian flag for superior beers, and applies the default template to the rest: using Windows.UI.Xaml But the entire template could also be generated on the spot through a XamlReader. The business logic could be as simple a selecting an existing template from a resource, like this.DefaultTemplate in the following code snippet. Once your class is defined you can assign an instance of the class to the template selector property of your element. The most powerful way of dynamically changing XAML, is using a DataTemplateSelector, which allows you to choose or generate a DataTemplate based on the data object and the data-bound element. To create a template selector, create a class that inherits from DataTemplateSelector and override the SelectTemplate method. Hopefully the problem will be solved next week, with the Release Preview. I did not elaborate this example to much, since unfortunately style selectors don't work in the Consumer Preview. You can then assign the ItemContainerStyleSelector to an instance of it: (new Setter(ListViewItem.ForegroundProperty, new SolidColorBrush(Colors.Red))) Īgain, the class containing the logic should be defined as a resource: Style style = new Style(typeof(ListViewItem)) Protected override Style SelectStyleCore(object item, DependencyObject container)
#Uwp colorconverter how to#
Here's a small example on how to modify the style for a list view item: public class BeerStyleSelector: StyleSelector The bound item is the first parameter, the item's style is the return value. This provides a way to apply styles based on custom logic. This seems to be the only way to create a dynamic style in Metro, due to the absence of DataTriggers. Just create a subclass of the StyleSelector class and implement the SelectStyle method. Using a Style SelectorĪnother way of dynamically styling items in a XAML list control, is with a StyleSelector. That definitely restricts the usage of a value converter, compared to WPF and Silverlight. Unfortunately Metro doesn't ship with MultiBinding, and bindings don't come with a StringFormat option. Just define an instance of the converter as a resource: Public object ConvertBack(object value, Type targetType, object parameter, string language) Public object Convert(object value, Type targetType, object parameter, string language) Public class BeerColorConverter : IValueConverter Here's an example of a string-to-string converter that translates official beer colors into HTML named colors: using System A value converter is a class that implements the IValueConverter interface, with just Convert and ConvertBack functions. Sometimes a conversion of value and/or data type needs to take place that can be done through a ValueConverter. Here's a screenshot of the attached sample project:ĭependency properties of a XAML control can be directly bound to properties of the viewModel. In this article, I will discuss the following techniques: value converters, style selectors, and data template selectors. When working in Metro with XAML and data, you will want to maximally leverage the data binding capabilities of the platform from day one.
#Uwp colorconverter windows 8#
This article describes how the Windows 8 Metro Consumer Preview deals with three standard ways of dynamically applying a look-and-feel to XAML controls.
