使用触发器更改 xaml 中的图像源无法正常工作

发布于 2024-11-01 09:42:23 字数 726 浏览 0 评论 0原文

我需要根据视图模型中的某些布尔属性更改工具栏中的图像。我正在使用触发器来更改图像源。这是正确的方法吗?我的代码运行不正常,有时可以工作,但有时图像保持不变。

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-11-08 09:42:23

它应该有效。如果没有其余的代码,很难理解为什么它不会。您是否在具有 IsAnalysisRunning 属性的任何类中实现 INotifyPropertyChanged 接口?

这是我用来测试这个的一个小示例:

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文