What is the replacement for DataTrigger in Silverlight

This is my scenario.

I have 2 Properties. Type and State.

Type is an Enum with 3 values eg, ball, car, arrow. State is an int which would accept 3 state values eg., -1, 0, 1. Also, I have 9 images for each state values.

Like, if I select type as ball and value as -1, I want to display a Red color ball. If I select type as arrow and value as 1, I want to display a up arrow. etc.,

I'm able to do this in WPF. I created 3 DataTemplates with an empty Image. Then, I use DataTrigger to check and update the particular image for the selected StateValue.

But, in silverlight how can I do this. I know, I have to do it in VSM. But, I would like to know some more details regarding this (or) any alternatives available.

-------------Problems Reply------------

I'd just use a converter that takes your object with 2 properties and returns an image. Code like that in pure XAML is painful and really belongs in C#.

I'd use GoToState behaviors with DataTriggers in Silverlight. Pretty simple in Blend:

Put all of your logic for what drives you to a different state in your view model. Expose the state as an enum. Open the States tab. Create a new state group (if you don't already have one). Create your states. From the Assets tab, select Behaviors. Drag the GoToState behavior from the Assets tab and drop it on your root visual element. In the Properties panel, click the "New" button next to the TriggerType and select DataTrigger. Remember that enum on your view model? Set the Trigger Binding to the state enum on the view model. Set the Trigger Value to the value of the enum. Set the StateName to the target state.

Blend should now have generated all of the VSM XAML for you. Once you get the hang of things you'll see how in some scenarios you don't even need the enum on the view model -- you'll be able to drive the state entirely off of the view.

To expand on Mike Post's post here's the XAML in case you don't have Blend.

You need to add references to Microsoft.Expression.Interactions and System.Windows.Interactivity.


Then in your control, at the same level as the VisualStateManager put this:

<ia:DataTrigger Binding="{Binding PropertyName}" Value="PropertyValue" >
<ia:GoToStateAction StateName="StateName" />

The blog post "Expression SDK in Silverlight–DataTrigger Example" covers it pretty well. Here is a sample of what he does:

<ia:DataTrigger Binding="{Binding IsEnabled}" Comparison="Equal" Value="false">
<ia:ControlStoryboardAction Storyboard="{StaticResource DisableStoryboard}"></ia:ControlStoryboardAction>

<ia:DataTrigger Binding="{Binding IsEnabled}" Comparison="Equal" Value="true">
<ia:ControlStoryboardAction Storyboard="{StaticResource EnableStoryboard}"></ia:ControlStoryboardAction>

(With the two XML namespace prefixes i and ia being defined as follows:)


Category:silverlight Views:0 Time:2010-08-20

Related post

  • How to replace current control in Silverlight/WP7 with a transition? 2011-03-07

    I have a Wp7 that has two controls. One is a map, and the other is a panorama control. In one of the panorama items is a list of values. If a user clicks on a list item, I'd like to show a turnstile transition to a new control that shows more detail.

  • How to replace target of binding (silverlight 4.0) 2011-11-28

    Problem description: In my Silverlight application (4.0), I have a TextBox control, which background is bound to some source property. I created a behavior for TextBox control. Inside my behavior, I want to take that binding, and "redirect" to other

  • Can SilverLight Replace ASP.net 2010-08-30

    i am new to web development and i am learning Asp.net . i was the other day thinking and i came up with this question . i searched the web but all the answers i found were old so my question is , can silverlight4 replace Asp.net ?? I know this questi

  • Access Silverlight's createObject via Javascript's new operator? 2009-08-21

    feel free to skip down to the question below I'm trying to create a bridge so a huge javascript application can be replaced by a new Silverlight-based rewrite as smoothly as possible. In the legacy application, there is this JS class: function LatLng

  • Handling view state in Silverlight with MVVM 2010-02-23

    I am interested to know how you people out there handle the view state in a Silverlight application with the MVVM pattern. Let's say I have a simple search mask that asynchronously calls a webservice. While the search is in progress, I'd like to chan

  • Choosing between WPF and Silverlight 2010-04-08

    We have an existing web application developed using ASP.NET/Ajax We are planning to move it to either WPF or Silverlight. Can someone please compare these 2 technologies with respect to productivity,performance, maintainability,trade-offs, their pros

  • How to create a WPF-like data trigger in Silverlight? 2011-01-02

    How might i create a trigger for a Silverlight datagrid in which the cell background color changes based on the cell value? I worked on a WPF project sometime ago and I recall this was quite simple via DataTriggers in the xaml. However this functiona

  • Portable Class Library - target Silverlight 5 2012-02-16

    The "change target framework" dialog in the portable class library properties has a link pointing to this page: http://msdn.microsoft.com/en-us/hh487282.aspx Said page indicates that there is a silverlight 5 'targeting pack' included in 'Silverlight

  • What do I lose by uninstalling Silverlight? It keeps crashing Chrome 2014-08-23

    Whenever I try to use Hotmail on Google Chrome, silverlight crashes the session. Never had a problem until the last MS update. Is there another SW that replaces the functionality of Silverlight (without the crashes - please) ? --------------Solutions

  • How do I make WPF TextBox acted as a compact form? 2010-10-13

    How do I make WPF TextBox acted as a compact form? This means that "label text" inside TextBox is hiding on the click or when there is "real text". See working prototype --------------Solutions------------- You can use a data trigger in the style for

  • Design for multi-tenant app using SOA, UoW, Repository, DataContext & multiple databases 2010-11-30

    Let me start by apologizing for the length of this post but I wanted to provide as much detail as possible to increase the chance of an answer. Thanks in advance. I’m adding new features to an existing application that integrates data from several da

  • Migrate a Desktop legacy accounting application from desktop to Web, How/Approach/Patterns? 2011-04-13

    I have a need to migrate a legacy, desktop based, single user, accounting application to a web based, with rich UI(possibly), multi user one. It is more or less re-writing the entire legacy system. And that too as QUICKLY as possible. Can somebody ma

  • ListPicker inside PanoramaItem doesn't work on SDK v7.1 2011-08-30

    I use ListPicker from Silverlight Toolkit for WP August 2011 as below but it doesn't work <controls:PanoramaItem Header="settings"> <toolkit:ListPicker> <toolkit:ListPickerItem Content="5" /> <toolkit:ListPickerItem Content="10"

  • Replacing the decimal point key (from numpad) with correct decimal separator (in silverlight!) 2008-12-04

    What I am trying to do when the user is in a textbox (in silverlight 2.0): When user presses the decimal point (.) on the numeric pad, I want to have it replaced by the correct decimal separator (which is comma (,) in a lot of countries) I can track

  • Will plug-ins such as Flash, Silverlight, etc. eventually replace XHTML/CSS/Javascript? 2009-01-12

    I've been developing with XHTML, CSS and Javascript for about 4 years now. I love it a lot and hate it a little. I've looked into Flash and Silverlight a bit, but as a developer, I'm not too keen on them. One reason is that they lock you into a vendo

  • Silverlight changing styles based on an objects property value (ie DataTrigger) 2009-09-27

    Does anyone have a successful workaround for changing a style in silverlight based on a property of the underlying data object, in that when the value changes so does the style. I used WPF briefly and it obviously has the DataTrigger which seems to c

  • Has silverlight replaced or killed off XBAPS 2009-12-11

    Has silverlight replaced or killed off XBAPS? --------------Solutions------------- I guess you are refering to the Silverlight 4 and its extended priviledges. Well it certainly eats into some area where you would have had to use an XBAP before. Howev

  • Does HTML5 only replace the video aspects of Flash/Silverlight? 2010-04-21

    I see a lot of talk how HTML5 video tag will kill Flash. But while video is the most widely used part of Flash/SL, it's only a small part of their technical abilities. For instance you can write a game using full 3D graphics and socket connections in

  • Do Silverlight and AJAX play complementary roles or will one replace the other? 2010-05-18

    AJAX is the big thing...but it seems (to me) that with Silverlight 4 Microsoft has offered a really robust method for creating UI interfaces for the web. I'm wondering - what is the future of AJAX? Do AJAX and Silverlight complement each other or wil

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.203 (s). 11 q(s)