图像源绑定不显示任何内容

时间:2017-09-27 20:49:01

标签: c# wpf xaml

我有一个以这种方式构建的图像:

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

为什么会发生这种情况?

2 个答案:

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