【问题标题】:Image Source Binding not display anything图像源绑定不显示任何内容
【发布时间】:2017-09-27 20:49:01
【问题描述】:

我的图像结构如下:

<Image Height="32" Width="32" Source="{Binding MatchController.Match.TeamHomeShield}" IsEnabled="False" />

还有一个标签:

<Label Content="{Binding MatchController.Match.TeamHomeShield}" />

我的问题是在Image上看不到图片,但是在label上可以看到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();
            }
        }

为什么会这样?

【问题讨论】:

  • TeamHomeShield = "http://cache.images.core.optasports.com/soccer/teams/150x150/1270.png"一起为我工作

标签: c# wpf xaml


【解决方案1】:

请检查您的图片来源格式 "/YourAssemblyName;component/YourPath/YourImage with extension"

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

}

【讨论】:

  • 图片来自网络,不存在绑定问题
【解决方案2】:

您可以使用 ImageSource 属性(不是字符串)正确绑定图像。

例如:

public ImageSource ImagePath { get; set; } 

【讨论】:

  • 编译器在ImageSource下划线,好像不识别是什么原因?
  • @Vandehusend System.Windows.Media.ImageSource
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 2020-08-29
  • 2014-04-27
相关资源
最近更新 更多