我有一个以这种方式构建的图像:
<Image Height="32" Width="32" Source="{Binding MatchController.Match.TeamHomeShield}" IsEnabled="False" />
和标签:
<Label Content="{Binding MatchController.Match.TeamHomeShield}" />
我的问题是我无法获取图像上显示的图像,但在标签上我可以看到TeamHomeShield
的值,该属性是以这种方式创建的:
private string _teamHomeShield;
public string TeamHomeShield
{
get { return _teamHomeShield; }
set
{
_teamHomeShield = value;
OnPropertyChanged();
}
}
private string _teamAwayShield;
public string TeamAwayShield
{
get { return _teamAwayShield; }
set
{
_teamAwayShield = value;
OnPropertyChanged();
}
}
为什么会发生这种情况?
答案 0 :(得分:0)
请检查您的图像来源格式 &#34; / YourAssemblyName;组件/ YourPath / YourImage,扩展名为&#34;
XAML:
<Grid>
<Image Source="{Binding ImagePath}" Width="200" Height="100"/>
</Grid>
xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = objviewmodel;
objviewmodel.ImagePath = @"/ImageLoading;component/Assets/Desert.jpg"; // your image path
}
viewmodel objviewmodel = new viewmodel();
}
public class viewmodel : INotifyPropertyChanged
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnpropertyChanged([CallerMemberName] string PropertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
#endregion
private string _ImagePath=string.Empty;
public string ImagePath
{
get { return _ImagePath; }
set { _ImagePath = value; OnpropertyChanged(); }
}
}
答案 1 :(得分:0)
您可以使用 ImageSource 属性(非字符串)正确绑定图片。
例如:
public ImageSource ImagePath { get; set; }