前提
C# WPF(Prism使用)にて、16個のチェックボックスを格納したItemsControlを作成しました。
実現したいこと
ItemsControl内のCheckboxに変更があった際、変更されたチェックボックスにバインドされている
Bitクラスのプロパティ(Area,BitNumber,IsSet)を使用して、EventNumberオブジェクトを生成する処理を行いたい。
該当のソースコード
Xaml
<Window x:Class="Sample.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" xmlns:viewmodels="clr-namespace:Sample.ViewModels" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d:DataContext="{d:DesignInstance Type=viewmodels:MainWindowViewModel}" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" prism:ViewModelLocator.AutoWireViewModel="True" Title="Sample" Height="1000" Width="1000" > <Grid> <ContentControl prism:RegionManager.RegionName="ContentRegion" /> <Grid> <ItemsControl x:Name="Area1" ItemsSource="{Binding Collection}" > <ItemsControl.ItemTemplate> <DataTemplate> <CheckBox IsChecked="{Binding IsSet,Mode=TwoWay}" FlowDirection="RightToLeft"> <TextBlock > <Run Text=""/><Run Text="{Binding BitNumber, Mode=OneTime}"/> </TextBlock> </CheckBox> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Rows="1"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </Grid> </Grid> </Window>
ViewModel.cs
using Sample.Models; using Prism.Mvvm; using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; namespace Sample.ViewModels { public class MainWindowViewModel : BindableBase { public MainWindowViewModel() { } private string _eventNumber; public string EventNumber { get { return _eventNumber; } set { SetProperty(ref _eventNumber, value); } } /// <summary> /// ItemsControl生成用ObservableCollection /// </summary> public ObservableCollection<Bit> Collection { get; } = new ObservableCollection<Bit>(Enumerable.Range(0, 16).Select(v => new Bit(v,1))); private DelegateCommand _collectionChanged; public DelegateCommand CollectionChanged => _changed ?? (_collectionChanged = new DelegateCommand(ExecuteCollectionChanged)); void ExecuteCollectionChanged() { //_eventNumberを生成したい。コマンドだと変更された要素を特定できない? } } }
Bit.cs
using Prism.Mvvm; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sample.Models { public class Bit : BindableBase { private bool _isSet; public int Area { get; } public int BitNumber { get; } public bool IsSet { get { return _isSet; } set { SetProperty(ref _isSet, value); } } public Bit(int bitNumber,int area) { BitNumber = bitNumber; Area = area; } } }
EventNumber.cs
using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata.Ecma335; using System.Text; using System.Threading.Tasks; namespace Sample.Models { internal class EventNumber { private readonly int _value; public int Value { get { return _value; } } public EventNumber(int wordArea, int inputMap, bool contact) { } } }
試したこと
コマンドでできそうな気がしましたが、変更された要素のみを渡す処理が思いつきません。
それともコマンドを使用するという発想から間違えているのでしょうか?
自分の検索力の無さによる疑問かもしれませんが、ご教授お願い致します。
補足情報(FW/ツールのバージョンなど)
Windows10
Visual Studio2022
Prism 8.1.97

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