tags:

views:

20

answers:

2

Hi.

I have created a DataGridCellTemplate where I have an Image control. By default it's Source property is X. I fill DataGrid with objects of my own class (have also implemented INotifyPropertyChanged interface).

I would like to change Source property of Image control when some boolean variable changes from False to True.

Should I use a trigger? If yes, how? Or maybe it should be done in c# code?

I could make 2 image controls, bind and control their Visible property, but it's lame solution I think.

Would appreciate any help.

A: 

You should see if a Converter will do what you're wanting. You write one in code by creating a class which implements the IValueConverter interface (MSDN has an example on their site).

You would then declare the ValueConverter as a StaticResource like the following (you'll have to declare the local namespace if you don't already have it):

<local:BoolToImageConverter x:Key="imageConverter" />

To use it, you then bind the ImageControl's Source property to the Boolean property and specify the converter in the binding. An example follows:

<Image Source={Binding Path=IsImageShown, Converter={StaticResource imageConverter}} />

One more thing to be aware of is that the converter cannot just return a string containing a URI to an image location. It should return an ImageSource such as a BitmapImage.

Eric
Will check this solution later, below looks easier and works. Thank you anyway.
pavel
A: 

In your DataTemplate try the following:

<DataTemplate>
    <Image Name="Image" Source="X"/>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding BooleanProperty}" Value="True">
            <Setter Property="Source" TargetName="Image" Value="Y" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

Where BooleanProperty is the property that triggers the source-shift. Note that the Image must have a name - and that should be used in the Setter-tag. In the example - I change the source from 'X' to 'Y'

Hope this helps!

Goblin
That's exactly what I expected! Thx!
pavel