2017-02-10 3 views
1

コードから9つのボタンに背景色と9つの前景画像を追加します。 WPF/xamlではなくC#から画像を変更したいと思います。 背景色を使用して[OK]を動作します:ボタンに画像を挿入する前景

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

Windowsフォーム使用して簡単な解決策を持っています

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。 * .pngの[ビルドアクション]プロパティを[コンテンツ]に設定していることを確認してください。

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; 

ありがとうございました。

関連する問題