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

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

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

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

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

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

Q&A

解決済

1回答

5885閲覧

XAMLアプリ:複数のComboBoxに動的に値を追加したい

akatukio

総合スコア5

C#

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

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

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

0グッド

0クリップ

投稿2020/01/23 03:26

前提・実現したいこと

XAMLアプリのComboBoxのリストに値を動的に追加したいと考えております。
ただし同じ選択が出来るComboBoxが複数あり、
(実際はComboBoxはもっと数多くあり、一つ一つItemSourceをセットしていく事は難しい)

1つのItemSourceの更新で複数のComboBoxに同時に反映する必要があります。
Window.ResourcesからBindingする事で、同じ選択値を持つComboBoxを作る事は出来ましたが、
動的に追加する事がどうしてもできずにおり、方法を教えて頂けないでしょうか。

該当のソースコード

XAML

1<Window x:Class="WpfApp1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="200" Width="400"> 9 <Window.Resources> 10 <local:TestList x:Key="testlist" /> 11 </Window.Resources> 12 <Grid> 13 <StackPanel Orientation="Horizontal" > 14 <Button Click="Button_Click" Content="ButtonA" Width="100" /> 15 <ComboBox x:Name="combo1" VerticalContentAlignment="Center" DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource testlist}}" IsSynchronizedWithCurrentItem="False" Width="100"/> 16 <ComboBox x:Name="combo2" VerticalContentAlignment="Center" DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource testlist}}" IsSynchronizedWithCurrentItem="False" Width="100"/> 17 <ComboBox x:Name="combo3" VerticalContentAlignment="Center" DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource testlist}}" IsSynchronizedWithCurrentItem="False" Width="100"/> 18 </StackPanel> 19 </Grid> 20</Window> 21

該当のソースコード

C#

1using System.Collections.Generic; 2using System.Windows; 3 4namespace WpfApp1 5{ 6 public partial class MainWindow : Window 7 { 8 public MainWindow() 9 { 10 InitializeComponent(); 11 } 12 13 private void Button_Click(object sender, RoutedEventArgs e) 14 { 15 var testlist = FindResource("testlist") as TestList; 16 testlist.Add(new Test() { Name = "4", Val = "ddddd" }); // <--このボタンクリックによりComboBoxに値を追加したい。 17 } 18 } 19 20 public class TestList : List<Test> 21 { 22 public TestList() 23 { 24 this.Add(new Test() { Name = "1", Val = "aaaaa" }); 25 this.Add(new Test() { Name = "2", Val = "bbbbb" }); 26 this.Add(new Test() { Name = "3", Val = "CCCCC" }); 27 } 28 } 29 30 public class Test 31 { 32 public string Name { get; set; } 33 public string Val { get; set; } 34 } 35}

補足情報(FW/ツールのバージョンなど)

.NetFramework 4.5

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

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

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

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

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

guest

回答1

0

ベストアンサー

変更通知付きのコレクションを使ってください。
public class TestList : List<Test>

public class TestList : System.Collections.ObjectModel.ObservableCollection<Test>

投稿2020/01/23 04:07

hihijiji

総合スコア4150

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

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

akatukio

2020/01/23 04:14

ありがとうございます。変更したところ出来ました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問