Skip to main content

Microsoft Silverlight

Answered Question How to display different states in DataTemplateRSS Feed

(0)

joergw
joergw

Member

Member

1 points

2 Posts

How to display different states in DataTemplate

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

IanBlackburn
IanBlack...

Contributor

Contributor

2333 points

371 Posts

Answered Question

Re: How to display different states in DataTemplate

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}}" />
 

 

(If this has answered your question, "Mark as Answer" - many thanks)

Cheers

Ian Blackburn
SilverlightForBusiness.net

umarkashmiri
umarkash...

Member

Member

44 points

14 Posts

Re: How to display different states in DataTemplate

 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



Please mark it as answer if this reply helps you


SE,CTO 24/7,Lahore,Pakistan

joergw
joergw

Member

Member

1 points

2 Posts

Re: How to display different states in DataTemplate

Hi,

thanks for the fast replay. Yes a converter was exactly what I was looking for.

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities