概要
C# WPF 初心者です。
用語の誤用/的外れな質問ありましたらご容赦ください。
データバインディングを勉強しています。
リストボックス→別のリストボックスへのバインド方法をご教示願います。
質問事項_詳細
[表題]と[Memo]の要素を入力して追加ボタンを押すと、左側のリストボックスに入力内容が追加されます。
左側のリストボックスは[表題]のみ表示します。
下記画像のようにリストを選択すると、右側のリストボックスに
選択した項目の[表題]と[要素]が表示されるような仕組みにしたいです。
■画像の例
[表題]:ABC , [Memo]:DEF と入力した場合、右側のリストボックスに
ABC
DEF
と表示されるようにしたい。
以下3点ご教示願います。
- リストボックスの選択されている項目を取得する方法
- 取得した項目をバインドする方法
- そもそも右側はリストボックスではなく、他のコントロールを使用した方が良いのか
※また、ソースコードに改善点ありましたらご指摘願います。
該当のソースコード
XAML
1<Window x:Class="リストボックスSample.Views.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:prism="http://prismlibrary.com/" 5 prism:ViewModelLocator.AutoWireViewModel="True" 6 xmlns:l="clr-namespace:リストボックスSample.ViewModels" 7 Title="{Binding Title}" Height="350" Width="525" > 8 9 <Window.DataContext> 10 <l:MainWindowViewModel /> 11 </Window.DataContext> 12 13 <Grid> 14 <Grid.ColumnDefinitions> 15 <ColumnDefinition/> 16 <ColumnDefinition Width="5"/> 17 <ColumnDefinition Width="2*"/> 18 </Grid.ColumnDefinitions> 19 <Grid.RowDefinitions> 20 <RowDefinition Height="Auto"/> 21 <RowDefinition Height="5"/> 22 <RowDefinition Height="12*"/> 23 </Grid.RowDefinitions> 24 <TextBox x:Name="txt_Title" Grid.Column="0" Margin="43,5,10,4" Text="{Binding Txt_Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap"/> 25 <TextBox x:Name="txt_Memo" Grid.Column="2" Margin="66,5,122,5" Text="{Binding Txt_Memo,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap"/> 26 <Label x:Name="lbl_Title" Grid.Row="0" Grid.Column="0" Content="表題" HorizontalAlignment="Left" Margin="10,9,0,8" Width="35"/> 27 <Label x:Name="lbl_Memo" Grid.Row="0" Grid.Column="2" Content="Memo" Margin="5,10,286,6"/> 28 <Button Content="追加" Command="{Binding ExecuteCommand}" Grid.Column="2" Margin="0,9,10,10" HorizontalAlignment="Right" Width="56"/> 29 30 <!--タスク一覧--> 31 <ListBox x:Name="lst_Title" ItemsSource="{Binding Path=TaskList}" DisplayMemberPath="Title" IsSynchronizedWithCurrentItem="True" Margin="10,5,10,15" Grid.Row="2"/> 32 33 <!--詳細表示--> 34 <ListBox x:Name="lst_Detail" Grid.Column="2" Grid.Row="2" Margin="5,5,16,15"> 35 <ListBox.ItemTemplate> 36 <DataTemplate> 37 <StackPanel> 38 <!--該当部分(ここにTitleとMemoの要素を出力したい)--> 39 <TextBlock Text="" FontWeight="Bold"/> 40 <TextBlock Text=""/> 41 </StackPanel> 42 </DataTemplate> 43 </ListBox.ItemTemplate> 44 </ListBox> 45 46 </Grid> 47</Window> 48
C#
1// 2//ViewModel 3// 4 5using Prism.Mvvm; 6using Prism.Commands; 7using リストボックスSample.Views; 8using System.Windows.Documents; 9using System; 10using System.Collections.Generic; 11using System.Collections.ObjectModel; 12 13namespace リストボックスSample.ViewModels 14{ 15 public class MainWindowViewModel : BindableBase 16 { 17 private string txt_Title; 18 public string Txt_Title 19 { 20 get { return this.txt_Title; } 21 set 22 { 23 if(this.SetProperty(ref this.txt_Title, value)) 24 this.ExecuteCommand.RaiseCanExecuteChanged(); 25 } 26 } 27 28 private string txt_Memo; 29 public string Txt_Memo 30 { 31 get { return this.txt_Memo; } 32 set 33 { 34 if(this.SetProperty(ref this.txt_Memo, value)) 35 this.ExecuteCommand.RaiseCanExecuteChanged(); 36 } 37 } 38 39 public ObservableCollection<Task> TaskList { get; set; } 40 41 public DelegateCommand ExecuteCommand { get; private set; } 42 43 public MainWindowViewModel() 44 { 45 this.TaskList = new ObservableCollection<Task>(); 46 47 this.ExecuteCommand = new DelegateCommand(this.Execute, this.CanExecute); 48 } 49 50 //実行コマンド 51 private void Execute() 52 { 53 this.TaskList.Add(new Task { Title = Txt_Title, Detail = Txt_Memo }); 54 55 Txt_Title = ""; 56 Txt_Memo = ""; 57 58 this.ExecuteCommand.RaiseCanExecuteChanged(); 59 } 60 61 //コマンド条件式 62 private bool CanExecute() 63 { 64 if(string.IsNullOrEmpty(Txt_Title)) 65 { 66 return false; 67 } 68 return true; 69 } 70 71 } 72}
C#
1// 2//Model 3// 4 5using System; 6using System.Collections.Generic; 7using System.Text; 8 9namespace リストボックスSample.Views 10{ 11 public class Task 12 { 13 public string Title { get; set; } 14 public string Detail { get; set; } 15 } 16}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。