teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

見直しキャンペーン中

2023/07/23 04:01

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -1,175 +1,175 @@
1
- > 選択されているというのが重要な情報の場合(ファイルに選択情報も保存して次回復元する等)は、別の手のほうがいいかもしれません。
2
-
3
- > Taskクラスにbool IsSelectedプロパティを用意して、lst_TitleではListBoxItemのIsSelectedにバインドし、lst_DetailではListBoxItemのVisibilityにバインドします。
4
- > どちらのListBoxもTaskListがソースですが、lst_Detailでは未選択のものは無いように見えます。
5
- > ちょっとずるいようですが、割とポピュラーな手段だと思います。もし気になるようでしたら追記しますので言ってください。
6
-
7
- 1万字に収まらなかったのでこちらで失礼します。
8
-
9
- ```xaml
10
- <Window
11
- x:Class="Questions284783.Views.MainWindow"
12
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
13
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
14
- xmlns:controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
15
- xmlns:prism="http://prismlibrary.com/"
16
- Width="525"
17
- Height="350"
18
- prism:ViewModelLocator.AutoWireViewModel="True">
19
- <Window.Resources>
20
- <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
21
- </Window.Resources>
22
- <Grid>
23
- <Grid.ColumnDefinitions>
24
- <ColumnDefinition />
25
- <ColumnDefinition Width="2*" />
26
- </Grid.ColumnDefinitions>
27
- <Grid.RowDefinitions>
28
- <RowDefinition Height="Auto" />
29
- <RowDefinition />
30
- </Grid.RowDefinitions>
31
- <Grid>
32
- <Grid.ColumnDefinitions>
33
- <ColumnDefinition Width="Auto" />
34
- <ColumnDefinition />
35
- </Grid.ColumnDefinitions>
36
- <Label
37
- Margin="5"
38
- VerticalAlignment="Center"
39
- Content="表題" />
40
- <TextBox
41
- Grid.Column="1"
42
- Margin="5"
43
- VerticalAlignment="Center"
44
- Text="{Binding Txt_Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
45
- TextWrapping="Wrap" />
46
- </Grid>
47
- <Grid Grid.Column="1">
48
- <Grid.ColumnDefinitions>
49
- <ColumnDefinition Width="Auto" />
50
- <ColumnDefinition />
51
- <ColumnDefinition Width="Auto" />
52
- </Grid.ColumnDefinitions>
53
- <Label
54
- Margin="5"
55
- VerticalAlignment="Center"
56
- Content="Memo" />
57
- <TextBox
58
- Grid.Column="1"
59
- Margin="5"
60
- VerticalAlignment="Center"
61
- Text="{Binding Txt_Memo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
62
- TextWrapping="Wrap" />
63
- <Button
64
- Grid.Column="2"
65
- MinWidth="56"
66
- Margin="5"
67
- Command="{Binding ExecuteCommand}"
68
- Content="追加" />
69
- </Grid>
70
- <Grid Grid.Row="1">
71
- <Grid.RowDefinitions>
72
- <RowDefinition Height="Auto" />
73
- <RowDefinition />
74
- </Grid.RowDefinitions>
75
- <ComboBox
76
- x:Name="comboBox"
77
- Margin="5"
78
- SelectedIndex="0">
79
- <controls:SelectionMode>Single</controls:SelectionMode>
80
- <controls:SelectionMode>Multiple</controls:SelectionMode>
81
- <controls:SelectionMode>Extended</controls:SelectionMode>
82
- </ComboBox>
83
- <ListBox
84
- x:Name="lst_Title"
85
- Grid.Row="1"
86
- Margin="5"
87
- DisplayMemberPath="Title"
88
- IsSynchronizedWithCurrentItem="True"
89
- ItemsSource="{Binding TaskList}"
90
- SelectionMode="{Binding SelectedValue, ElementName=comboBox}">
91
- <ListBox.ItemContainerStyle>
92
- <Style TargetType="ListBoxItem">
93
- <Setter Property="IsSelected" Value="{Binding IsSelected}" />
94
- </Style>
95
- </ListBox.ItemContainerStyle>
96
- </ListBox>
97
- </Grid>
98
- <ListBox
99
- Grid.Row="1"
100
- Grid.Column="1"
101
- Margin="5"
102
- ItemsSource="{Binding TaskList}">
103
- <ListBox.ItemContainerStyle>
104
- <Style TargetType="ListBoxItem">
105
- <Setter Property="Visibility" Value="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" />
106
- </Style>
107
- </ListBox.ItemContainerStyle>
108
- <ListBox.ItemTemplate>
109
- <DataTemplate>
110
- <StackPanel>
111
- <TextBlock FontWeight="Bold" Text="{Binding Title}" />
112
- <TextBlock Text="{Binding Detail}" />
113
- </StackPanel>
114
- </DataTemplate>
115
- </ListBox.ItemTemplate>
116
- </ListBox>
117
- </Grid>
118
- </Window>
119
- ```
120
-
121
- ```C#
122
- using System.Collections.ObjectModel;
123
- using Prism.Commands;
124
- using Prism.Mvvm;
125
-
126
- namespace Questions284783.ViewModels
127
- {
128
- public class MainWindowViewModel : BindableBase
129
- {
130
- private string txt_Title;
131
- public string Txt_Title
132
- {
133
- get => txt_Title;
134
- set { if(SetProperty(ref txt_Title, value)) ExecuteCommand.RaiseCanExecuteChanged(); }
135
- }
136
-
137
- private string txt_Memo;
138
- public string Txt_Memo
139
- {
140
- get => txt_Memo;
141
- set { if(SetProperty(ref txt_Memo, value)) ExecuteCommand.RaiseCanExecuteChanged(); }
142
- }
143
-
144
- public ObservableCollection<Task> TaskList { get; } = new ObservableCollection<Task>();
145
- public DelegateCommand ExecuteCommand { get; private set; }
146
-
147
- public MainWindowViewModel()
148
- {
149
- TaskList.Add(new Task { Title = "aaa", Detail = "111" });
150
- TaskList.Add(new Task { Title = "bbb", Detail = "222" });
151
- TaskList.Add(new Task { Title = "ccc", Detail = "333" });
152
- ExecuteCommand = new DelegateCommand(Execute, CanExecute);
153
- }
154
-
155
- private void Execute()
156
- {
157
- TaskList.Add(new Task { Title = Txt_Title, Detail = Txt_Memo });
158
- Txt_Title = "";
159
- Txt_Memo = "";
160
- ExecuteCommand.RaiseCanExecuteChanged();
161
- }
162
-
163
- private bool CanExecute() => !string.IsNullOrEmpty(Txt_Title);
164
- }
165
-
166
- public class Task : BindableBase
167
- {
168
- public string Title { get; set; }
169
- public string Detail { get; set; }
170
-
171
- private bool isSelected;
172
- public bool IsSelected { get => isSelected; set => SetProperty(ref isSelected, value); }
173
- }
174
- }
1
+ > 選択されているというのが重要な情報の場合(ファイルに選択情報も保存して次回復元する等)は、別の手のほうがいいかもしれません。
2
+
3
+ > Taskクラスにbool IsSelectedプロパティを用意して、lst_TitleではListBoxItemのIsSelectedにバインドし、lst_DetailではListBoxItemのVisibilityにバインドします。
4
+ > どちらのListBoxもTaskListがソースですが、lst_Detailでは未選択のものは無いように見えます。
5
+ > ちょっとずるいようですが、割とポピュラーな手段だと思います。もし気になるようでしたら追記しますので言ってください。
6
+
7
+ 1万字に収まらなかったのでこちらで失礼します。
8
+
9
+ ```xml
10
+ <Window
11
+ x:Class="Questions284783.Views.MainWindow"
12
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
13
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
14
+ xmlns:controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
15
+ xmlns:prism="http://prismlibrary.com/"
16
+ Width="525"
17
+ Height="350"
18
+ prism:ViewModelLocator.AutoWireViewModel="True">
19
+ <Window.Resources>
20
+ <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
21
+ </Window.Resources>
22
+ <Grid>
23
+ <Grid.ColumnDefinitions>
24
+ <ColumnDefinition />
25
+ <ColumnDefinition Width="2*" />
26
+ </Grid.ColumnDefinitions>
27
+ <Grid.RowDefinitions>
28
+ <RowDefinition Height="Auto" />
29
+ <RowDefinition />
30
+ </Grid.RowDefinitions>
31
+ <Grid>
32
+ <Grid.ColumnDefinitions>
33
+ <ColumnDefinition Width="Auto" />
34
+ <ColumnDefinition />
35
+ </Grid.ColumnDefinitions>
36
+ <Label
37
+ Margin="5"
38
+ VerticalAlignment="Center"
39
+ Content="表題" />
40
+ <TextBox
41
+ Grid.Column="1"
42
+ Margin="5"
43
+ VerticalAlignment="Center"
44
+ Text="{Binding Txt_Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
45
+ TextWrapping="Wrap" />
46
+ </Grid>
47
+ <Grid Grid.Column="1">
48
+ <Grid.ColumnDefinitions>
49
+ <ColumnDefinition Width="Auto" />
50
+ <ColumnDefinition />
51
+ <ColumnDefinition Width="Auto" />
52
+ </Grid.ColumnDefinitions>
53
+ <Label
54
+ Margin="5"
55
+ VerticalAlignment="Center"
56
+ Content="Memo" />
57
+ <TextBox
58
+ Grid.Column="1"
59
+ Margin="5"
60
+ VerticalAlignment="Center"
61
+ Text="{Binding Txt_Memo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
62
+ TextWrapping="Wrap" />
63
+ <Button
64
+ Grid.Column="2"
65
+ MinWidth="56"
66
+ Margin="5"
67
+ Command="{Binding ExecuteCommand}"
68
+ Content="追加" />
69
+ </Grid>
70
+ <Grid Grid.Row="1">
71
+ <Grid.RowDefinitions>
72
+ <RowDefinition Height="Auto" />
73
+ <RowDefinition />
74
+ </Grid.RowDefinitions>
75
+ <ComboBox
76
+ x:Name="comboBox"
77
+ Margin="5"
78
+ SelectedIndex="0">
79
+ <controls:SelectionMode>Single</controls:SelectionMode>
80
+ <controls:SelectionMode>Multiple</controls:SelectionMode>
81
+ <controls:SelectionMode>Extended</controls:SelectionMode>
82
+ </ComboBox>
83
+ <ListBox
84
+ x:Name="lst_Title"
85
+ Grid.Row="1"
86
+ Margin="5"
87
+ DisplayMemberPath="Title"
88
+ IsSynchronizedWithCurrentItem="True"
89
+ ItemsSource="{Binding TaskList}"
90
+ SelectionMode="{Binding SelectedValue, ElementName=comboBox}">
91
+ <ListBox.ItemContainerStyle>
92
+ <Style TargetType="ListBoxItem">
93
+ <Setter Property="IsSelected" Value="{Binding IsSelected}" />
94
+ </Style>
95
+ </ListBox.ItemContainerStyle>
96
+ </ListBox>
97
+ </Grid>
98
+ <ListBox
99
+ Grid.Row="1"
100
+ Grid.Column="1"
101
+ Margin="5"
102
+ ItemsSource="{Binding TaskList}">
103
+ <ListBox.ItemContainerStyle>
104
+ <Style TargetType="ListBoxItem">
105
+ <Setter Property="Visibility" Value="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" />
106
+ </Style>
107
+ </ListBox.ItemContainerStyle>
108
+ <ListBox.ItemTemplate>
109
+ <DataTemplate>
110
+ <StackPanel>
111
+ <TextBlock FontWeight="Bold" Text="{Binding Title}" />
112
+ <TextBlock Text="{Binding Detail}" />
113
+ </StackPanel>
114
+ </DataTemplate>
115
+ </ListBox.ItemTemplate>
116
+ </ListBox>
117
+ </Grid>
118
+ </Window>
119
+ ```
120
+
121
+ ```cs
122
+ using System.Collections.ObjectModel;
123
+ using Prism.Commands;
124
+ using Prism.Mvvm;
125
+
126
+ namespace Questions284783.ViewModels
127
+ {
128
+ public class MainWindowViewModel : BindableBase
129
+ {
130
+ private string txt_Title;
131
+ public string Txt_Title
132
+ {
133
+ get => txt_Title;
134
+ set { if(SetProperty(ref txt_Title, value)) ExecuteCommand.RaiseCanExecuteChanged(); }
135
+ }
136
+
137
+ private string txt_Memo;
138
+ public string Txt_Memo
139
+ {
140
+ get => txt_Memo;
141
+ set { if(SetProperty(ref txt_Memo, value)) ExecuteCommand.RaiseCanExecuteChanged(); }
142
+ }
143
+
144
+ public ObservableCollection<Task> TaskList { get; } = new ObservableCollection<Task>();
145
+ public DelegateCommand ExecuteCommand { get; private set; }
146
+
147
+ public MainWindowViewModel()
148
+ {
149
+ TaskList.Add(new Task { Title = "aaa", Detail = "111" });
150
+ TaskList.Add(new Task { Title = "bbb", Detail = "222" });
151
+ TaskList.Add(new Task { Title = "ccc", Detail = "333" });
152
+ ExecuteCommand = new DelegateCommand(Execute, CanExecute);
153
+ }
154
+
155
+ private void Execute()
156
+ {
157
+ TaskList.Add(new Task { Title = Txt_Title, Detail = Txt_Memo });
158
+ Txt_Title = "";
159
+ Txt_Memo = "";
160
+ ExecuteCommand.RaiseCanExecuteChanged();
161
+ }
162
+
163
+ private bool CanExecute() => !string.IsNullOrEmpty(Txt_Title);
164
+ }
165
+
166
+ public class Task : BindableBase
167
+ {
168
+ public string Title { get; set; }
169
+ public string Detail { get; set; }
170
+
171
+ private bool isSelected;
172
+ public bool IsSelected { get => isSelected; set => SetProperty(ref isSelected, value); }
173
+ }
174
+ }
175
175
  ```

1

流れの説明

2020/08/15 07:55

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -1,7 +1,11 @@
1
+ > 選択されているというのが重要な情報の場合(ファイルに選択情報も保存して次回復元する等)は、別の手のほうがいいかもしれません。
2
+
3
+ > Taskクラスにbool IsSelectedプロパティを用意して、lst_TitleではListBoxItemのIsSelectedにバインドし、lst_DetailではListBoxItemのVisibilityにバインドします。
4
+ > どちらのListBoxもTaskListがソースですが、lst_Detailでは未選択のものは無いように見えます。
5
+ > ちょっとずるいようですが、割とポピュラーな手段だと思います。もし気になるようでしたら追記しますので言ってください。
6
+
1
7
  1万字に収まらなかったのでこちらで失礼します。
2
8
 
3
- IsSelectedにバインドパターン
4
-
5
9
  ```xaml
6
10
  <Window
7
11
  x:Class="Questions284783.Views.MainWindow"