Xaml 视图模型属性更改时如何更改图像源

Xaml 视图模型属性更改时如何更改图像源,xaml,mvvm,windows-phone-8,winrt-xaml,Xaml,Mvvm,Windows Phone 8,Winrt Xaml,我正在用MVVM Light构建一个WP8应用程序,我遇到了一个愚蠢的问题,我找不到简单的解决方案 在XAML页面中,我有以下内容: <Button Command="{Binding PlayCommand}" > <Image Source="Images/play.png"/> </Button> 执行PlayCommand时,我将ViewModel中的IsPlaying属性的值更改为true

我正在用MVVM Light构建一个WP8应用程序,我遇到了一个愚蠢的问题,我找不到简单的解决方案

在XAML页面中,我有以下内容:

        <Button Command="{Binding PlayCommand}" >
            <Image Source="Images/play.png"/>
        </Button>

执行PlayCommand时,我将ViewModel中的IsPlaying属性的值更改为true。 现在,我想让XAML页面对iPlay的更改做出反应,并将图像源更改为“Images/pause.png”。在WPF中,我可以使用DataTriggers(也不方便),但在WP8中似乎不起作用。当然,我可以像这样将图像源绑定到ViewModel,但这超出了MVVM的全部用途

    <Button Command="{Binding PlayCommand}" >
        <Image Source="{Binding PlayButtonImage}"/>
    </Button>


我知道有复杂的解决方案(例如,附加的属性、行为等),但我认为必须有一个简单的解决方案…

可能有这样一个转换器类

public class BoolToIconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool)
        {
            bool isPlaying = (bool)value;
            if (isPlaying)
                return "Images/pause.png";
            else 
                return "Images/play.png"
        } 
        return "Images/play.png";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
像这样使用

<Button Command="{Binding PlayCommand}" >
    <Image Source="{Binding IsPlaying, Converter={StaticResource BoolToIconConverter}}"/>
</Button>


虽然我不认为MVVM中的属性会破坏您的MVVM模式,甚至可能比使用值转换器提供更好的性能。

可能是这样的转换器类

public class BoolToIconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool)
        {
            bool isPlaying = (bool)value;
            if (isPlaying)
                return "Images/pause.png";
            else 
                return "Images/play.png"
        } 
        return "Images/play.png";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
像这样使用

<Button Command="{Binding PlayCommand}" >
    <Image Source="{Binding IsPlaying, Converter={StaticResource BoolToIconConverter}}"/>
</Button>


虽然我不认为MVVM中的属性会破坏您的MVVM模式,甚至可能比使用值转换器提供更好的性能。

您的ViewModel中的Image属性没有问题。

您的ViewModel中的Image属性没有问题。

您可以使用Source属性在图像中放置BitmapImage,例如:

<Button Command="{Binding PlayCommand}" >
    <Image>
        <Image.Source>
            <BitmapImage UriSource="{Binding ImageUri}"/>
        </Image.Source>
    </Image>
</Button>


然后,您可以将ImageUri绑定到ViewModel上Uri类型的通知属性。

您可以使用源属性在图像中放置位图图像,如下所示:

<Button Command="{Binding PlayCommand}" >
    <Image>
        <Image.Source>
            <BitmapImage UriSource="{Binding ImageUri}"/>
        </Image.Source>
    </Image>
</Button>


然后,您可以将ImageUri绑定到ViewModel上Uri类型的通知属性。

您可以在ViewModel中创建一个属性,如
Uri Playbutonimage
,MVVMI不会有任何问题,只需使用2个图像,并使用bool到可见性转换器绑定到图像可见性属性。您可以在ViewModel类似于
Uri PlayButtonImage
,MVVMI不会有任何问题,只需使用2个图像,并使用布尔到可见性转换器绑定到图像可见性属性。我想你是对的。。。这就是为什么它被称为ViewModel而不是模型。谢谢。我想你是对的。。。这就是为什么它被称为ViewModel而不是模型。谢谢。谢谢你详尽的回答。我知道我可以写一个转换器,这正是我想要避免的东西。您关于不打破MVVM模式的评论可能是正确的。我只是太正直了。谢谢你的彻底回答。我知道我可以写一个转换器,这正是我想要避免的东西。您关于不打破MVVM模式的评论可能是正确的。我只是太正直了。