2017-02-10 7 views
1

코드에서 9 개의 버튼에 배경색과 9 개의 전경 이미지를 추가하고 싶습니다. WPF/xaml이 아닌 C#에서 이미지를 변경하고 싶습니다.Button Foreground에 이미지 삽입

button1.Background.SetValue(SolidColorBrush.ColorProperty, Windows.UI.Colors.Red); 

윈도우 양식을하고있다 사용하기 쉬운 솔루션을 :

pictureBox1.Image = Properties.Resources.P1; // this does not work for UWP 

내가 지금까지 시도 오류 메시지가 끝나는 것을 : 를 내가 빌드 작업 속성을 변경 한 배경 색상 사용하여 작동 확인 P1.png를 PRIResource로 복사하십시오.

string url = "../../Images/P1.png"; 
//string url = "PW.png"; 
image1.Source = new BitmapImage(new Uri(url, UriKind.Relative)); //.Uri cannot be converted into a Windows.Foundation.Uri. 
//image1.Source = new BitmapImage(new Uri(url, UriKind.Absolute)); //format of url could not be determined 
<Button x:Name="button1" Content="Button" Grid.Column="0" Grid.Row="0" 
       Tag="1" Background="Gray" Padding="0" UseLayoutRounding="False" 
       HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="button1_Click"> 
    <Button.Foreground> 
     <ImageBrush Stretch="Fill" ImageSource="P1.PNG"/> 
    </Button.Foreground> 
</Button> 
+0

xaml에서 수행하는 것과 동일한 코드를 수행하려고 시도 했습니까? - 새로운 ImageBrush()를 만들고, * ImageSource *를 설정 한 다음, 버튼에 연결합니까? – Romasz

+0

[전경 및 배경이 이미지 인 [wpf 버튼]의 가능한 복제본] (http://stackoverflow.com/questions/4107813/wpf-button-with-foreground-and-background-as-image) –

+0

안녕하세요 Romasz. ImageSource를 설정하고 단추를 부착하는 방법에 대한 샘플 코드를 가르쳐 주시겠습니까? 감사합니다 – GolfCalcs

답변

0

솔루션 : 가 VS2015, UWP 및 Windows 8.1을 사용하여 C#에서 버튼에 이미지를 삽입하려면 :

이 버튼의 전면에 이미지를 추가합니다. 예 : Name = button1 및 imageX. "Build Action"* .png 속성을 Content로 설정했는지 확인하십시오.

enter code hereImageBrush imageBrush = new ImageBrush(); 
     imageBrush.ImageSource = new BitmapImage(new Uri("ms-appx:///Images/PW.PNG")); 
     button1.Background = imageBrush; 
BitmapImage bitImage = new BitmapImage(); 
     bitImage.UriSource = new Uri("ms-appx:///Images/PW.PNG"); 
     imageX.Source = bitImage; 

Romasz에게 도움을 주셔서 감사합니다.