Image Source Binding not display anything

2019-09-20 06:25发布

I've an image structured in this way:

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

and a label:

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

my problem's that I can't get the image displayed on the Image, but on the label I can see the value of TeamHomeShield, the property is created in this way:

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

why happen this?

标签: c# wpf xaml
2条回答
何必那么认真
2楼-- · 2019-09-20 06:43

You can use ImageSource property (not string) for binding images correctly.

For example:

public ImageSource ImagePath { get; set; } 
查看更多
倾城 Initia
3楼-- · 2019-09-20 06:47

Please check your Image source format "/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(); }
    }

}
查看更多
登录 后发表回答