Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit How to display different states in DataTemplate
3 replies. Latest Post by joergw on October 31, 2008.
(0)
joergw
Member
1 points
2 Posts
10-31-2008 7:47 AM |
Hi,
I have a collection of Data-Objects, which i have bound to a listBox. Now i want to display the states of each item, for example to change the opacity or color depending on the data item. In WPF i would use Triggers within the DataTemplate. For some reason, in Silverlight this is not possible.
So, how could I change the view in the DataTemplate?
<DataTemplate> <Grid> <Border Name="MainBorder" Background="#FF0000" Opacity="0.00" /> <TextBlock Text="{Binding Path=Title}" /> </Grid></DataTemplate>
How can I change the Opacity of this Border "MainBorder"???
<ListBox ItemsSource="{SomeKindOfDataSource}" ItemsTemplate="{StaticResource SeeDataTemplateAbove}" />
Thanks a lot
Jörg
IanBlack...
Contributor
2333 points
371 Posts
10-31-2008 8:04 AM |
Probably a ValueConverter is the way to go.
Create a new Class that implements IValueConverter:
public class BorderOpacityConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string myValue = (string)value; switch (myValue) { case "One": return 0.00; case "Two": return 0.50; case "Three": return 1.00; default: return 0; } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion }
Add it as a resource to your page:
<UserControl x:Class="SilverlightDataBinding.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SilverlightDataBinding" Width="400" Height="300">
<UserControl.Resources> <local:BorderOpacityConverter x:Name="BorderOpacityConverter"></local:BorderOpacityConverter> </UserControl.Resources> ...
Use it in a binding for the property:
<Border Name="MainBorder" Background="#FF0000" Opacity="{Binding Title,Converter={StaticResource BorderOpacityConverter}}" />
umarkash...
44 points
14 Posts
no dear you have to make a story board for animation and set its target to that border name.When you did this then on listbox loaded event start this story board. ie. stroyBoardName.begin();
<br />
I hope this would help you
10-31-2008 8:33 AM |
thanks for the fast replay. Yes a converter was exactly what I was looking for.