locked
Dynamic selection of an Image Source through DataBinding RRS feed

  • Question

  • I have a status column on a spreadsheet control.  It is an enumeration.  I want a different picture in the status column depending on the value of a 'Status' enumeration value behind the scenes.  Here's what I've got so far:

     
     

    Code Snippet

    <!-- Status Style -->

    <Style x:Key="StatusStyle">

    <Style.Triggers>

    <Trigger Property="Image.Tag" Value="Buy">

    <Setter Property="Image.Source" Value="{StaticResource BuyImage}" />

    </Trigger>

    <Trigger Property="Image.Tag" Value="Sell">

    <Setter Property="Image.Source" Value="{StaticResource SellImage}" />

    </Trigger>

    </Style.Triggers>

    </Style>

     

     

    <Image Style="{StaticResource StatusStyle}" Tag="{Binding Path=Content}"/>

     

     

    The trouble is that the 'Tag' in the System.Image class is an System.Object, so XAML has no way of interpreting the "Buy" or "Sell" text to be an enumeration.  The only datatype that this trigger seem to understand is a string.  Is there any way to 'Force' a conversion in XAML.  Something like a 'Cast' operation?

     

    <Trigger Property="Image.Tag" Value="{Cast {X:Type Status}, Sell}">

    Thursday, March 20, 2008 12:45 PM

Answers

  • you could also rewrite your value part like this:

    <Trigger.Value>
       <localTongue Tiedtatus>Buy</localTongue Tiedtatus>
    </Trigger.Value>

    where local maps to the namespace that defines your enumerator.
    Friday, March 21, 2008 8:28 AM

All replies

  • Have you tried using DataTriggers?

     

    <Style.Triggers>

      <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Tag}" Value="Buy">

        <Setter Property="Image.Source" Value="{StaticResource BuyImage}" />

      </DataTrigger>

      <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Tag}" Value="Sell">

        <Setter Property="Image.Source" Value="{StaticResource SellImage}" />

      </DataTrigger>

    </Style.Triggers>

    Thursday, March 20, 2008 4:18 PM
  • you could also rewrite your value part like this:

    <Trigger.Value>
       <localTongue Tiedtatus>Buy</localTongue Tiedtatus>
    </Trigger.Value>

    where local maps to the namespace that defines your enumerator.
    Friday, March 21, 2008 8:28 AM
  • Bragi,

    Thanks a million.  That did the trick.

     

    Don

     

    Friday, March 21, 2008 11:32 AM
  • Fellas,

     

    I have a slight variation of this problem. My code is something like the following:

     

    <Button

    x:Name="buttonSort"

    ToolTip="{Binding Tag, RelativeSource={RelativeSource Self}}"

    Tag="Default"

    Click="OnSort_Changed"

    >

    <Image x:Name="imageSort" Source="{StaticResource DefaultSort}">

    <Image.Style>

    <Style TargetType="{x:Type Image}">

    <Style.Triggers>

    <DataTrigger

    Binding="{Binding Tag, ElementName=buttonSort}"

    Value="Ascending">

    <Setter Property="Image.Source"

    Value="{StaticResource AscendingSort}"/>

    </DataTrigger>

    <DataTrigger

    Binding="{Binding Tag, ElementName=buttonSort}"

    Value="Descending">

    <Setter Property="Image.Source"

    Value="{StaticResource DescendingSort}"/>

    </DataTrigger>

    </Style.Triggers>

    </Style>

    </Image.Style>

    </Image>

    </Button>

     

    My handler is simple:

     

    enum ResourceSortDirection

    {

    Default = 0,

    Ascending = 1,

    Descending = 2

    }

     

    void OnSort_Changed(object sender, RoutedEventArgs e)

    {

    Button sortButton = sender as Button;

    if (sortButton != null)

    {

    MySortDirection sort =

    (MySortDirection)Enum.Parse(typeof(MySortDirection), sortButton.Tag.ToString());

    int sortIndex = Convert.ToInt32(sort);

    //Does stuff

    sort = (MySortDirection)(++sortIndex % 3);

    //Does more stuff

    sortButton.Tag = sort.ToString();

    }

    }

     

    I bind the tooltip to the tag to verify/debug that the binding has occurred, and it does, so I know the value is updated, but either my data trigger is not functioning or my resource is incorrect, which I do not think is the case.

     

    Point being, my source value is not updating on the image. Am I doing something wrong?

     

    Thanks in advance,

     

    Andres Olivares

    Friday, March 21, 2008 6:28 PM