单击按钮时更新ImageSource

时间:2013-06-02 20:10:54

标签: c# xaml

当我加载并填充ListBox时,我会检查bool值是true还是false,并且取决于我想要的bool绑定当前图像(likes.png或notliked.png)。

列表框中的

按钮:

<Button Click="LikePost">
    <Button.Background>
        <ImageBrush Stretch="Uniform" ImageSource="{Binding imagesource}"/>
    </Button.Background>
</Button>
<Image Source="liked.png" Visibility="collapsed"/>
<Image Source="notliked.png" Visibility="collapsed"/>

(只有当最后两行出现时,图像显示???)

我将以下类分配给列表框:

public class Item : INotifyPropertyChanged
{
    public string value1 { get; set; }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    private System.Windows.Media.ImageSource _imagesource;
    public System.Windows.Media.ImageSource imagesource
    {
        get { return _imagesource; }
        set
        {
            if (_imagesource == value) return;
            _imagesource = value;
            NotifyLikeImageChanged("like");
        }
    }
    private void NotifyLikeImageChanged(string propertyName)
    {
        System.ComponentModel.PropertyChangedEventHandler handler = PropertyChanged;
        if (PropertyChanged != null)
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
    }
}

在我的LikePost函数中,我检查帖子是否被喜欢并相应地更改我班级中的imagesource。但图像不会改变?

1 个答案:

答案 0 :(得分:0)

你可以试试这个。

如果您的silverlight应用程序位于/ ClientBin文件夹下,则该URL应该相对于ClientBin。因此,如果您的图片直接位于您的网络应用程序下,您可以尝试新的Uri(“../ path.png”,UriKind.Relative)。

您还可以使用fiddler或chrome开发人员工具(网络标签)来捕获您的图片请求,该请求会显示您要求的网址。