Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing image source in xaml with trigger does not work properly

I need to change image in toolbar according to some boolean property in my viewModel. I am using trigger to change image source. Is it the right way? My code is not running properly, sometimes it works, but sometimes the image stay unchanged.

<Image x:Key="startPauseResumeAnalysisToolbarImage" >
        <Image.Style>
            <Style TargetType="{x:Type Image}">
                <Setter Property="Source" Value="Resources/ToolbarIcons/play.png" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsAnalysisRunning}" Value="True" >
                        <Setter Property="Source" Value="Resources/ToolbarIcons/pause.png"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
</Image>
like image 352
Stepan Benes Avatar asked Apr 14 '11 08:04

Stepan Benes


1 Answers

It should work. Difficult to see why it does not without the rest of the code. Are you implementing the INotifyPropertyChanged interface in whatever class has the IsAnalysisRunning property?

Here is a small sample i used to test this:

MainWindow.xaml

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:WpfApplication2"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
    <Image >
        <Image.Style>
            <Style TargetType="{x:Type Image}">
                <Setter Property="Source" Value="Desert.jpg" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsAnalysisRunning}" Value="True" >
                        <Setter Property="Source" Value="Koala.jpg"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="0,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

MainWindow.xaml.cs:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }
    private bool _isAnalysisRunning = false;
    public bool IsAnalysisRunning
    {
        get { return _isAnalysisRunning; }
        set {
            _isAnalysisRunning = value;
            NotifyPropretyChanged("IsAnalysisRunning");
        }
    }
    private void NotifyPropretyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        IsAnalysisRunning = !IsAnalysisRunning;
    }
}
like image 126
Kjetil Watnedal Avatar answered Nov 15 '22 10:11

Kjetil Watnedal