質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

2952閲覧

【WPF】複数のImageコントロールにそれぞれ画像を表示したい、また画像を切り替えたい

ksgtksa43

総合スコア2

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

1グッド

0クリップ

投稿2021/12/03 06:49

前提・実現したいこと

WPFで、複数のButtonコントロールとそれに対応した複数のImageコントロールがあるアプリを作っています。ButtonコントロールとImageコントロールはXAMLで定義したものを、cs側で配列のように扱えるように読み込んでいます。Button[0],Image[0]のような感じで。
各Button.Contentには識別のために文字列が入っています。

ここで、Button[x]をクリックしたときにImage[x]に画像が動的に表示されるようにしたいです。また、クリック回数で場合分けして画像が切り替わるようにもしたいです。
(表示なし)→画像AAA.png→画像BBB.png→(表示なし)→以下ループ
といった感じに。

以下コードになります。

C#

1 2 private int[] count = new int[5];//ボタンのクリック回数をリストに保存 3 private System.Windows.Controls.Button[] button_ = new System.Windows.Controls.Button[5]; 4 private System.Windows.Controls.Image[] image_ = new System.Windows.Controls.Image[5]; 5 6 private void button_Click(object sender, RoutedEventArgs e) 7 { 8 //int x; 9 if (count[x] == 0) //クリック回数が0回 10 { 11 image_[x] = new Image(); 12 image_[x].Visibility = Visibility.Visible; 13 BitmapImage bi = new BitmapImage(); 14 bi.BeginInit(); 15 bi.UriSource = new Uri("/Resources/AAA.png", UriKind.Relative); 16 bi.EndInit(); 17 image_[x].Source = bi; 18 count[x]++; 19 } 20 else if (count[x] == 1)//クリック回数が1回 21 { 22 image_[x] = new Image(); 23 image_[x].Visibility = Visibility.Visible; 24 BitmapImage bi = new BitmapImage(); 25 bi.BeginInit(); 26 bi.UriSource = new Uri("/Resources/BBB.png", UriKind.Relative); 27 bi.EndInit(); 28 image_[x].Source = bi; 29 count[x]++; 30 } 31 else if (count[x] == 2)//クリック回数が2回 32 { 33 image_[x].Visibility = Visibility.Hidden; 34 count[x] = 0; 35 } 36     } 37 38 private void Window_Loaded(object sender, RoutedEventArgs e) 39 { 40 for (int i = 0; i < 5; i++)//各コントロールを配列化 41 { 42 button_[i] = this.FindName("Button_" + i.ToString()) as System.Windows.Controls.Button; 43 image_[i] = this.FindName("ButtonImage_" + i.ToString()) as System.Windows.Controls.Image; 44 } 45 }

XAML

1    //座標情報など省きます。 2 <Image x:Name="ButtonImage_0" IsHitTestVisible="False"/> 3 ... 4 <Image x:Name="ButtonImage_4" IsHitTestVisible="False"/> 5 6 <Button x:Name="Button_0" Click="button_Click"> 7    ... 8 <Button x:Name="Button_4" Click="button_Click"> 9

これだと画像は表示されませんでした。
countが変わっているのは確認できたので、Buttonコントロールの配列化はできていると思います。Imageコントロールの方が参照されていないような気がしています。

初心者故、そもそもの前提知識もあやふやなところがあるので、Imageコントロールはこのように参照できない等の間違いがあればそちらも教えていただきたいです。

よろしくお願いします。

TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

image_[x] = new Image();こうしたところで、元のButtonImage_0等には何の影響もありません。

Imageはすでにxamlで配置しているわけですから新たに作る必要はなく、Sourceを変更するだけでいいのです^^

xml

1<Window 2 x:Class="Questions372108.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 Width="800" 6 Height="450"> 7 <StackPanel> 8 <UniformGrid Rows="1"> 9 <Button x:Name="Button_0" Click="Button_Click" Content="Button_0" /> 10 <Button x:Name="Button_1" Click="Button_Click" Content="Button_1" /> 11 <Button x:Name="Button_2" Click="Button_Click" Content="Button_2" /> 12 <Button x:Name="Button_3" Click="Button_Click" Content="Button_3" /> 13 <Button x:Name="Button_4" Click="Button_Click" Content="Button_4" /> 14 </UniformGrid> 15 16 <UniformGrid Rows="1"> 17 <Image x:Name="ButtonImage_0" IsHitTestVisible="False" /> 18 <Image x:Name="ButtonImage_1" IsHitTestVisible="False" /> 19 <Image x:Name="ButtonImage_2" IsHitTestVisible="False" /> 20 <Image x:Name="ButtonImage_3" IsHitTestVisible="False" /> 21 <Image x:Name="ButtonImage_4" IsHitTestVisible="False" /> 22 </UniformGrid> 23 </StackPanel> 24</Window>

cs

1using System; 2using System.Windows; 3using System.Windows.Controls; 4using System.Windows.Media.Imaging; 5 6namespace Questions372108 7{ 8 public partial class MainWindow : Window 9 { 10 private readonly int[] counts = new int[5]; 11 private readonly Button[] buttons = new Button[5]; 12 private readonly Image[] images = new Image[5]; 13 14 private Button Button_0_Test; 15 private Button currentButton; 16 17 public MainWindow() 18 { 19 InitializeComponent(); 20 21 for (var i = 0; i < 5; i++) 22 { 23 buttons[i] = FindName($"Button_{i}") as Button; 24 images[i] = FindName($"ButtonImage_{i}") as Image; 25 } 26 27 Button_0_Test = Button_0; 28 } 29 30 private void Button_Click(object sender, RoutedEventArgs e) 31 { 32 var x = Array.IndexOf(buttons, sender); 33 34 if (counts[x] == 0) 35 { 36 var bi = new BitmapImage(); 37 bi.BeginInit(); 38 //bi.UriSource = new Uri("/Resources/AAA.png", UriKind.Relative); 39 bi.UriSource = new Uri("https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg"); 40 bi.EndInit(); 41 42 images[x].Source = bi; 43 counts[x]++; 44 } 45 else if (counts[x] == 1) 46 { 47 var bi = new BitmapImage(); 48 bi.BeginInit(); 49 //bi.UriSource = new Uri("/Resources/BBB.png", UriKind.Relative); 50 bi.UriSource = new Uri("https://teratail-v2.storage.googleapis.com/uploads/avatars/u20/201856/m2cobCxI_thumbnail.jpg"); 51 bi.EndInit(); 52 53 images[x].Source = bi; 54 counts[x]++; 55 } 56 else if (counts[x] == 2) 57 { 58 images[x].Source = null; 59 counts[x] = 0; 60 } 61 62 63 // これが何の意味もない理由 64 //image_[x] = new Image(); 65 66 // 例えばこうしたとして、表示されているButton_0が入れ替わるわけではない! 67 Button_0_Test = new Button { Content = "Button_0がこうは変わらない!", }; 68 69 // 単に変数Button_0_Testの指す先が、新しいボタンに変わっただけ 70 // Button_0は依然表示されたまま 71 // 新たに作ったボタンはウィンドウに追加されていないので、 72 // 表示されずにただメモリ内にあるだけになっている 73 74 75 // こういった使い方なら意味はあるが 76 currentButton = sender as Button; 77 currentButton.Content = $"Button_{x}_{counts[x]}"; 78 //buttons[x].Content = $"Button_{x}_{counts[x]}"; 79 } 80 } 81}

アプリ画像

投稿2021/12/03 08:45

編集2023/07/29 10:35
TN8001

総合スコア9401

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ksgtksa43

2021/12/05 02:32

回答ありがとうございます。 ボタンクリックによる画像の表示および切り替えができるようになりました! また、new Image()についても分かりやすく解説していただきありがとうございます。勉強になります。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問