WPF - 버튼의 배경 이미지를 동적으로 변경하는 코드

WPF - 버튼의 배경 그림을 동적으로 교체하는 코드 기사
디렉토리
1 상대 경로
2 절대 경로 방법
2.1 그림을 프로젝트로 가져오지 않은 경우
2.2 그림을 프로젝트로 가져온 경우
1 상대 경로는
다음 위치에서 그림을 검색합니다. 프로젝트 exe 경로 자원 디렉토리

ImageBrush brush1 = new ImageBrush();
string path = @"Resource/icon-submit.png";
BitmapImage img = new BitmapImage(new Uri(path, UriKind.Relative));
brush1.ImageSource = img;
Testbutton.Background = brush1 ;

2 절대 경로 방식
2.1 이미지를 프로젝트로 가져오지 않은 경우
ImageBrush brush1 = new ImageBrush();
string path = @"F:/Resource/icon-submit.png";
BitmapImage img = new BitmapImage(new Uri(path , UriKind .Absolute));
brush1.ImageSource = img;
Testbutton.Background = brush1;

2.2 이미지를 프로젝트로 가져온 경우
@"pack://application: 플래그를 현재 프로젝트로 사용합니다.

ImageBrush brush1 = new ImageBrush();
문자열 경로 = @"pack://application:,,,/Resource/icon-提交.png";
BitmapImage img = new BitmapImage(new Uri(path, UriKind.Absolute));
brush1.ImageSource = img;
Testbutton.Background = brush1;
 

추천

출처blog.csdn.net/u014090257/article/details/130961919