1

Sourceコード内で画像をにバインドしBitmapImageていますが、表示されません。

xaml:

<Window x:Class="bleh.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="600" d:DesignWidth="600">
<Grid x:Name="LayoutRoot">
    <Image x:Name="current" HorizontalAlignment="Center" Stretch="None" VerticalAlignment="Center" Source="{Binding Picture}" />
</Grid>
</Window>

と私のcs:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    /// <summary>
    /// Event implementing INotifyPropertyChanged interface.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    public BitmapImage Picture { get; set; }

    public MainWindow()
    {
        Uri uri = new Uri("Images/xpto.jpg", UriKind.Relative);
        this.Picture = new BitmapImage(uri);
        InitializeComponent();
        //setup();
    }
}

不思議なことに、ウィンドウは画像のサイズで開きますが、画像が表示されません。また、xamlで手動で割り当ててみましたが、機能します。

やることcurrent.source="Images/xpto.jpg"もうまくいきます。

4

2 に答える 2

1

The DataContext of your view (MainWindow) is not set, so there is no "Picture" property to which to bind. If you want the view to bind to itself, add

this.DataContext = this;

to your MainWindow constructor.

于 2012-07-19T00:12:29.713 に答える
0

プロパティPictureにINotifyPropertyChangedを実装していません。publicBitmapImagePicture{get; セットする; あなたがしなければならないこと:

BitmapImage _picture;
public BitmapImage Picture {
get {return _picture;}
 set{
      _picture=value;
      OnPropertyChanged("Picture");
    }`
 }
于 2013-01-09T13:23:28.437 に答える