1

mvvm パターンを使用して wpf でアプリケーションを開発しています。

私のアプリケーションでは、画像を選択してフォームに表示し、データベースに保存する必要があります。

wpf フォームでは、イメージ コントロールを使用してイメージを表示しています。

ビュー モデルで、ファイル ダイアログを開き、Image プロパティを割り当てます。

BitmapImage image;
public BitmapImage Image
{
    get { return image; }
    set
    {
        image = value;
        RaisePropertyChanged("Image");
    }
}

...

OpenFileDialog file = new OpenFileDialog();
Nullable<bool> result =file.ShowDialog();

if (File.Exists(file.FileName))
{
    image = new BitmapImage();
    image.BeginInit();
    image.UriSource = new Uri(file.FileName, UriKind.Absolute);
    image.EndInit();
}

私のxaml部分は

 <Image Height="144" HorizontalAlignment="Left" Source="{Binding Image}"
        Margin="118,144,0,0" Name="imgData" Stretch="Fill" VerticalAlignment="Top" Width="340" />

フォームに画像が表示されません。どのように?

4

1 に答える 1

2

Imageフィールドではなく、プロパティを割り当てる必要がありimageます。それ以外の場合、PropertyChanged イベントは発生しません。

if (File.Exists(file.FileName))
{
    Image = new BitmapImage(new Uri(file.FileName, UriKind.Absolute));
}

の基本クラスであるImage型のプロパティを宣言することも意味があることに注意してください。これにより、 から派生した他のタイプのインスタンスをプロパティに割り当てることができます。ImageSourceBitmapImageImageSourceBitmapFrameWriteableBitmap

于 2013-11-05T07:00:15.893 に答える