前提・実現したいこと
WPFのMVVMパターンでアプリを作っています。
タブとアイテムを使ったウィンドウの作成をしていますが、各タブのアイテムの個数と中身を動的にしたいです。
該当のソースコード
xaml
1//View 2<Grid> 3 <Button Content="CountUp" HorizontalAlignment="Left" Height="76" Margin="72,57,0,0" VerticalAlignment="Top" Width="135" Click="OnClickCountUp"/> 4 <Button Content="OpenSubWindow" HorizontalAlignment="Left" Height="74" Margin="269,202,0,0" VerticalAlignment="Top" Width="123" Click="OnClicOpenSubWindow"/> 5 6 <Label Content="Count : " HorizontalAlignment="Left" Height="28" Margin="254,73,0,0" VerticalAlignment="Top" Width="53"/> 7 <Label Content="{Binding CountVal}" HorizontalAlignment="Left" Height="28" Margin="307,73,0,0" VerticalAlignment="Top" Width="67"/> 8</Grid>
C#
1//ViewModel 2public class MainViewModel : INotifyPropertyChanged 3{ 4 private string count; 5 public string CountVal 6 { 7 get { return count; } 8 set 9 { 10 count = value; 11 NotifyPropertyChanged("CountVal"); 12 } 13 14 } 15 public ObservableCollection<TabItemData> TabItems { get; set; } = new ObservableCollection<TabItemData>(); 16 17 18 public event PropertyChangedEventHandler PropertyChanged; 19 private void NotifyPropertyChanged(String info) 20 { 21 if (PropertyChanged != null) 22 { 23 PropertyChanged(this, new PropertyChangedEventArgs(info)); 24 } 25 } 26} 27 28public class TabItemData 29{ 30 public string Header { get; set; } 31 public ObservableCollection<CheckBox> Content { get; set; } 32}
C#
1//コードビハインド 2public partial class MainWindow : Window 3{ 4 private MainViewModel mViewmodel; 5 int count = 0; 6 7 public MainWindow() 8 { 9 InitializeComponent(); 10 mViewmodel = new MainViewModel(); 11 this.DataContext = mViewmodel; 12 } 13 14 private void OnClicOpenSubWindow(object sender, RoutedEventArgs e) 15 { 16 Window1 window1 = new Window1(mViewmodel); 17 window1.Show(); 18 } 19 20 private void OnClickCountUp(object sender, RoutedEventArgs e) 21 { 22 count++; 23 24 ObservableCollection<CheckBox> items = new ObservableCollection<CheckBox>(); 25 26 for (int i = 0; i <= count; i++) 27 { 28 CheckBox checkBox = new CheckBox(); 29 checkBox.IsChecked = false; 30 checkBox.Content = count.ToString(); 31 32 items.Add(checkBox); 33 } 34 35 mViewmodel.CountVal = count.ToString(); 36 mViewmodel.TabItems.Add(new TabItemData() { Header = count.ToString(), Content = items }); 37 } 38}
C#
1//サブウィンドウのView 2<Grid> 3 <TabControl ItemsSource="{Binding TabItems}"> 4 <TabControl.ItemTemplate> 5 <DataTemplate> 6 <TextBlock Text="{Binding Header}" /> 7 </DataTemplate> 8 </TabControl.ItemTemplate> 9 10 <TabControl.ContentTemplate> 11 <DataTemplate> 12 <CheckBox Content="{Binding Content}" /> 13 </DataTemplate> 14 </TabControl.ContentTemplate> 15 </TabControl> 16</Grid>
C#
1//サブウィンドウのコードビハインド 2public partial class Window1 : Window 3{ 4 public Window1(MainViewModel pMainViewModel) 5 { 6 InitializeComponent(); 7 this.DataContext = pMainViewModel; 8 } 9}
サンプルアプリなので若干適当ですが、例えばタブ1にチェックボックスを1つ、タブ2にチェックボックスを2つといったようにすることはできるでしょうか?
チェック回数に応じてチェックボックスの個数を変えていきたいです。
またできればでいいのですが、その際チェックボックスの選択状態を保持できるようにしたいです。
(サブウィンドウを再度開きなおしても閉じる前にチェックボックスがtrueだったものは、trueでサブウィンドウを表示したい)
ご助言いただけますと幸いです。
回答1件
あなたの回答
tips
プレビュー