C# 单击按钮时更新ImageSource
当我加载并填充我的C# 单击按钮时更新ImageSource,c#,xaml,C#,Xaml,当我加载并填充我的列表框时,我会检查bool值是true还是false,并根据该bool绑定当前图像(liked.png或notliked.png) 列表框中的按钮: <Button Click="LikePost"> <Button.Background> <ImageBrush Stretch="Uniform" ImageSource="{Binding imagesource}"/> </Button.Backgr
列表框时,我会检查bool
值是true
还是false
,并根据该bool
绑定当前图像(liked.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"/>
在LikePost函数中,我检查帖子是否受欢迎,并相应地更改类中的imagesource。但是图像不会改变?您可以试试这个
如果您的silverlight应用程序位于/ClientBin文件夹下,则url应与ClientBin相对。因此,如果您的图像位于web应用程序的正下方,您可以尝试新的Uri(“../path.png”,UriKind.Relative)
您还可以使用fiddler或chrome developer工具(网络选项卡)捕获图像请求,这将显示您请求的url。您有权访问此文件吗?为什么Absolute
而不是RelativeOrAbsolute
文件在我的根目录中。当我使用“绝对”时,我得到了“System.UriFormatException”。UriKind.RelativeOrAbsolute
很抱歉没有正确阅读您的答案。当我使用RelativeOrAbsolute时,没有显示图像。
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));
}
}