C#を勉強している初心者です。
下記サイトのコードを参考にしていて、試しに選択中のものをListBoxに表示させようとしたとき上手くいかずに詰まってしまったので質問させていただきました。
https://social.msdn.microsoft.com/Forums/ja-JP/c21a0bf4-46b8-4273-a810-255320d0ad3d/combobox12398369842524620013123982051612434viewmodel1236312425353732?forum=wpfja
ラベルに表示される内容と同じものをListBoxに表示させようとしましたが、1文字1文字分割されて数行で表示されてしまいます。
これを一行で表示する、コレクションの別の値を複数表示させるにはどのようにしたらいいのか分かる方ご教示ください。
よろしくお願いいたします。
XAML
1<Window x:Class="WpfTest1.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:WpfTest1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="300" Width="360" Name="UI"> 9 10 <Window.DataContext> 11 <local:MainWindowViewModel/> 12 </Window.DataContext> 13 14 <Grid> 15 <Grid.RowDefinitions> 16 <RowDefinition Height="1*"/> 17 <RowDefinition Height="1*"/> 18 <RowDefinition Height="1*"/> 19 </Grid.RowDefinitions> 20 <ComboBox x:Name="Maker" Margin="16" 21 ItemsSource="{Binding Path=Items}" SelectedItem="{Binding Path=Item,Mode=TwoWay}"> 22 <ComboBox.ItemTemplate> 23 <DataTemplate> 24 <StackPanel Orientation="Horizontal"> 25 <TextBlock Text="{Binding Path=Name}" Width="100"/> 26 <TextBlock Text="{Binding Path=TEL}"/> 27 </StackPanel> 28 </DataTemplate> 29 </ComboBox.ItemTemplate> 30 </ComboBox> 31 <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="16"> 32 <TextBlock Text="担当者:"/> 33 <TextBlock Text="{Binding ElementName=Maker, Path=SelectedItem.Tantou}"/> 34 </StackPanel> 35 <ListBox x:Name="ListBox1" ItemsSource="{Binding ElementName=Maker, Path=SelectedItem.Tantou}" HorizontalAlignment="Left" Height="69" Margin="123,10,0,0" Grid.Row="2" VerticalAlignment="Top" Width="78"> 36 </Grid> 37</Window>
C#
1using System; 2using System.Collections.Generic; 3using System.Collections.ObjectModel; 4using System.Linq; 5using System.Text; 6using System.Threading.Tasks; 7using System.ComponentModel; 8 9namespace WpfTest1 10{ 11 class Maker 12 { 13 public int Id { get; set; } 14 public string Name { get; set; } 15 public string TEL { get; set; } 16 public string Tantou { get; set; } //担当者 17 } 18 19 class MainWindowViewModel : INotifyPropertyChanged 20 { 21 private ObservableCollection<Maker> items = new ObservableCollection<Maker>(); 22 public ObservableCollection<Maker> Items 23 { 24 get { return items; } 25 set { items = value; } 26 } 27 28 private Maker item; 29 public Maker Item 30 { 31 get { return item; } 32 set 33 { 34 35 if (item == value) return; 36 item = value; 37 NotifyPropertyChanged("Item"); 38 } 39 } 40 41 public MainWindowViewModel() 42 { 43 //初期値 44 items = new ObservableCollection<Maker>{ 45 new Maker{Id=1,Name="YAMAHA",TEL="06-9931-1584",Tantou="紺野佐吉,堀江明里"}, 46 new Maker{Id=2,Name="NEC",TEL="07-903-6820",Tantou="古屋冨士夫,大江薫"}, 47 new Maker{Id=3,Name="バッファロー",TEL="0885-40-6796",Tantou="国分七美,宮坂理香"}, 48 new Maker{Id=4,Name="エレコム",TEL="02-2748-8396",Tantou="日比野美香,渡辺徳雄"}, 49 new Maker{Id=5,Name="IODATA",TEL="098-333-1626",Tantou="神谷基一,望月幸彦"}, 50 new Maker{Id=6,Name="Cisco",TEL="04-0380-5406",Tantou="岩渕和歌,三村国男"}, 51 new Maker{Id=7,Name="NetGear",TEL="024-450-5136",Tantou="鎌田絵理,細谷彩香"} 52 }; 53 } 54 55 56 public event PropertyChangedEventHandler PropertyChanged; 57 protected void NotifyPropertyChanged(String info) 58 { 59 if (PropertyChanged != null) 60 { 61 PropertyChanged(this, new PropertyChangedEventArgs(info)); 62 } 63 } 64
papinianus様にご提示いただいたコードでイメージしていた表示になりました。
他にもこんなやり方あるよという方いましたらご教示ください。
回答2件
あなたの回答
tips
プレビュー