[WPF] 직접 경로 이미지 연결

JEONGKI'S Note·2023년 2월 8일
0

1. ViewModel 클래스 생성: ViewModel 클래스를 생성하여 이미지의 경로를 관리합니다.

public class ViewModel
{
  private string _imagePath;
  public string ImagePath
  {
    get { return _imagePath; }
    set { _imagePath = value; }
  }

  public ViewModel()
  {
    ImagePath = @"C:\images\image1.jpg";
  }
}

2. XAML 코드 작성: XAML 코드를 작성하여 이미지 컨트롤을 생성합니다. 아래 예제를 참고하세요.

<Window x:Class="ImageDisplay.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Image Display" Height="350" Width="525">
  <Grid>
    <Image Source="{Binding ImagePath}"/>
  </Grid>
</Window>

3. C코드 작성: C# 코드를 작성하여 ViewModel 클래스와 XAML 코드를 연결합니다. 아래 예제를 참고하세요.

namespace ImageDisplay
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      DataContext = new ViewModel();
    }
  }
}

4. 실행: 프로젝트를 실행하여 직접 경로로 연결된 이미지를 확인할 수 있습니다.

profile
주니어 개발자 공부노트입니다 :)

0개의 댓글