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

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

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

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

XAML

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

Q&A

1回答

3733閲覧

c#でコンボボックス変更時に値を変更したい

netta

総合スコア43

C#

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

XAML

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

0グッド

0クリップ

投稿2021/12/21 01:12

前提・実現したいこと

c#でコンボボックスの値が変更された際に値を変更したいです。

発生している問題・エラーメッセージ

this.CbxPileNo.SelectedValue = "(abab)"
で値を設定するとselectedvalueがnullとなってしまいます。
1度登録したものは変更できないのでしょうか?

該当のソースコード

xaml

1 <ComboBox 2 Grid.Row="2" Grid.Column="3" 3 Grid.RowSpan="1" 4 Style="{StaticResource SimpleComboBoxStyle}" 5 x:Name="CbxPileNo" 6 Margin="10,0,0,0" 7 DisplayMemberPath="List1" 8 SelectedValuePath="List1" 9 SelectedValue="{Binding No, Mode=TwoWay, 10 UpdateSourceTrigger=PropertyChanged}" 11 SelectionChanged="CbxPileNo_SelectionChanged"/>

c#

1 private void CbxPileNo_SelectionChanged(object sender, SelectionChangedEventArgs e) 2 { 3 this.CbxPileNo.SelectedValue = "(abab)"; 4 } 5 6 //List1を登録している箇所 7 public void SetComboboxPileList() 8 { 9 try 10 { 11 // コンボボックスリスト設定 12 this.comboList.Add(new PersonalLibrary.Models.comboList() 13 { 14 List1 = "ー" 15 }); 16 17 for (int i = 0; i < this.PileInfoList.Count; i++) 18 { 19 // コンボボックスリスト設定 20 this.comboList.Add(new PersonalLibrary.Models.comboList() 21 { 22 23 List1 = this.PileInfoList[i].No 24 }); 25 } 26 } 27 catch (Exception ex) 28 { 29 // エラー出力 30 LibIOManager.OutputErr(ex, AppConstants.ErrorFile, MethodBase.GetCurrentMethod().Name); 31 } 32 } 33 34 //combolistのプロパティ 35 public ObservableCollection<PersonalLibrary.Models.comboList> comboList 36 { 37 get; 38 set; 39 } 40 = new ObservableCollection<PersonalLibrary.Models.comboList>();

試したこと

this.CbxPileNo.SelectedValue = "(abab)"
で値を設定するとselectedvalueがnullとなってしまいます。
1度登録したものは変更できないのでしょうか?

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

・visual studio 2017
・C#
・XAML
で開発しています。

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

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

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

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

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

guest

回答1

0

c#でコンボボックスの値が変更された際に値を変更したいです。

選択されたComboListの、List1を変えたいということでしょうか?

全部(abab)に変わってしまっては、訳が分からなくなると思うのですが(もちろん仮の話だということは分かっていますが、どういう意図かがさっぱりわかりません)

状態がわかりやすいようにListBoxにしていますが、ComboBoxでも同じです。

INotifyPropertyChangedに、↓を使いましたが何でもいいです。
NuGet Gallery | CommunityToolkit.Mvvm 7.1.2

xml

1<Window 2 x:Class="Questions374830.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="clr-namespace:Questions374830" 6 Width="800" 7 Height="450"> 8 <Window.DataContext> 9 <local:ViewModel /> 10 </Window.DataContext> 11 <StackPanel> 12 <!--<ComboBox 13 x:Name="CbxPileNo" 14 DisplayMemberPath="List1" 15 ItemsSource="{Binding ComboList}" 16 SelectedValue="{Binding No, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 17 SelectedValuePath="List1" 18 SelectionChanged="CbxPileNo_SelectionChanged" />--> 19 20 <ListBox 21 x:Name="CbxPileNo" 22 DisplayMemberPath="List1" 23 ItemsSource="{Binding ComboList}" 24 SelectedValue="{Binding No, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 25 SelectedValuePath="List1" 26 SelectionChanged="CbxPileNo_SelectionChanged" /> 27 28 <TextBlock Text="{Binding SelectedValue, ElementName=CbxPileNo, StringFormat=SelectedValue: {0}}" /> 29 <TextBlock Text="{Binding SelectedItem, ElementName=CbxPileNo, StringFormat=SelectedItem: {0}}" /> 30 <TextBlock Text="{Binding No, StringFormat=No: {0}}" /> 31 </StackPanel> 32</Window>

cs

1using System.Collections.ObjectModel; 2using System.Windows; 3using System.Windows.Controls; 4using CommunityToolkit.Mvvm.ComponentModel; 5 6namespace Questions374830 7{ 8 public class ComboList : ObservableObject 9 { 10 public string List1 { get => _List1; set => SetProperty(ref _List1, value); } 11 private string _List1; 12 13 public override string ToString() => $"Item {{ List1 = {List1} }}"; 14 } 15 16 public class ViewModel : ObservableObject 17 { 18 public ObservableCollection<ComboList> ComboList { get; } = new ObservableCollection<ComboList>(); 19 20 public string No { get => _No; set => SetProperty(ref _No, value); } 21 private string _No; 22 23 24 public ViewModel() => SetComboboxPileList(); 25 26 private void SetComboboxPileList() 27 { 28 ComboList.Add(new ComboList { List1 = "―", }); 29 30 for (var i = 0; i < 5; i++) 31 { 32 ComboList.Add(new ComboList { List1 = $"{i}", }); 33 } 34 } 35 } 36 37 public partial class MainWindow : Window 38 { 39 public MainWindow() => InitializeComponent(); 40 41 private void CbxPileNo_SelectionChanged(object sender, SelectionChangedEventArgs e) 42 { 43 //CbxPileNo.SelectedValue = "(abab)"; 44 ((ComboList)CbxPileNo.SelectedItem).List1 = "(abab)"; 45 } 46 } 47}

List1を変更したときはListBoxの見た目は変わるが、SelectedValueやそのバインド先は変わらない(変えたのはList1であって、ComboListではないため)
再選択したときには変わっている(うまく説明できないので動かしてみてください)
アプリ画像


ComboListのコレクションもComboListは非常に違和感があります(List<ComboItem> ComboListとかなら納得できます)
さらに中にList1がありますが、おそらく仮名でしょうから置いておきます(もし実際目にしたら「どんだけリストだよっ」と突っ込むと思いますw

投稿2021/12/21 08:59

編集2023/07/29 13:03
TN8001

総合スコア9401

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問