質問
ListViewにおいて複数選択した場合に、コードビハインドを使わずにアイテム群を取得したいです。
ビヘイビアを使用して取得しようと試みているのですが、複数選択した場合でも、
以下のコード内の「SelectedStudents」にアイテム群が入らずにnullのままです。
原因を教えていただけますようよろしくお願いいたします。
BM_StudentList.xaml(一部抜粋)
C#
1<UserControl x:Class="Presentation.BillingManagement.BM_StudentList" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:i="http://schemas.microsoft.com/xaml/behaviors" 5 xmlns:prism="http://prismlibrary.com/" 6 xmlns:local="clr-namespace:Presentation.BillingManagement" 7 prism:ViewModelLocator.AutoWireViewModel="True"> 8 <StackPanel Margin="10" > 9 <ListView ItemsSource="{Binding Students}" 10 Width="Auto" HorizontalContentAlignment="Stretch"> 11 <i:Interaction.Behaviors> 12 <local:SelectedItemsBehavior SelectedItems="{Binding SelectedStudents}" /> 13 </i:Interaction.Behaviors> 14 <ListView.View > 15 <GridView > 16 <GridViewColumn Header="番号" DisplayMemberBinding="{Binding Id}" Width="50"/> 17 <GridViewColumn Header="氏名" DisplayMemberBinding="{Binding Name}" Width="Auto" /> 18 <GridViewColumn Header="フリガナ" DisplayMemberBinding="{Binding Kana}" Width="Auto" /> 19 </GridView> 20 </ListView.View> 21 </ListView> 22 <Button Content="読込" Command="{Binding OkButton}" 23 HorizontalAlignment="Right" Width="60" Margin="10"/> 24 </StackPanel> 25</UserControl>
BM_StudentListViewModel.cs(一部抜粋)
C#
1using Prism.Commands; 2using Prism.Mvvm; 3using Prism.Services.Dialogs; 4using System; 5using System.Collections.ObjectModel; 6 7namespace Presentation.BillingManagement 8{ 9 public class BM_StudentListViewModel : BindableBase, IDialogAware 10 { 11 private StudentUsecase _usecase; 12 public BM_StudentListViewModel(StudentUsecase usecase) 13 { 14 _usecase = usecase; 15 Students = _usecase.GetStudents(); 16 OkButton = new DelegateCommand(OkButtonExecute); 17 } 18 19 private ObservableCollection<Student> _students 20 = new ObservableCollection<Student>(); 21 public ObservableCollection<Student> Students 22 { 23 get { return _students; } 24 set { SetProperty(ref _students, value); } 25 } 26 27 private ObservableCollection<Student> _selectedStudents; 28 public ObservableCollection<Student> SelectedStudents 29 { 30 get { return _selectedStudents; } 31 set { SetProperty(ref _selectedStudents, value); } 32 } 33 34 public DelegateCommand OkButton { get; } 35 private void OkButtonExecute() 36 { 37 var r = ButtonResult.OK; 38 if (SelectedStudents == null) 39 { 40 r = ButtonResult.None; 41 } 42 var p = new DialogParameters(); 43 p.Add(nameof(SelectedStudents), SelectedStudents); 44 RequestClose.Invoke(new DialogResult(r, p)); 45 } 46 } 47} 48
BM_Behavior.cs
C#
1using Microsoft.Xaml.Behaviors; 2using System.Collections; 3using System.Linq; 4using System.Windows; 5using System.Windows.Controls; 6using System.Windows.Controls.Primitives; 7 8namespace Presentation.BillingManagement 9{ 10 [TypeConstraint(typeof(Selector))] 11 public class SelectedItemsBehavior : Behavior<Selector> 12 { 13 public static readonly DependencyProperty SelectedItemsProperty = 14 DependencyProperty.Register("SelectedItems", 15 typeof(IEnumerable), 16 typeof(SelectedItemsBehavior), 17 new FrameworkPropertyMetadata(null, 18 FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 19 public IEnumerable SelectedItems 20 { 21 get => (IEnumerable)GetValue(SelectedItemsProperty); 22 set => SetValue(SelectedItemsProperty, value); 23 } 24 protected override void OnAttached() 25 { 26 base.OnAttached(); 27 AssociatedObject.SelectionChanged += SelectionChanged; 28 } 29 protected override void OnDetaching() 30 { 31 AssociatedObject.SelectionChanged -= SelectionChanged; 32 base.OnDetaching(); 33 } 34 private void SelectionChanged(object sender, SelectionChangedEventArgs args) 35 { 36 dynamic selector = AssociatedObject; 37 SelectedItems = Enumerable.ToArray(selector.SelectedItems); 38 } 39 } 40}
補足情報
C#
WPF
Prism.Unity
Prism.Wpf
Microsoft.Xaml.Behaviors.Wpf
を使用しています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/07/25 22:46