IsSynchronizedWithCurrentItem属性と現在のアイテムの更新
-
10-07-2019 - |
質問
リストのフィルタリング(必要な場合)およびアイテムの選択を可能にするダイアログタイプのビューを管理するビューモデルがあります。 IsSynchronizedWithCurrentItemをtrueに設定してもしなくても、コードは正常に機能します。私の理解では、このプロパティはListViewではデフォルトではtrueではないため、このプロパティをよりよく理解したいと思います。
ビューのxamlのバインディング設定は次のとおりです(synchプロパティ設定なしでも同様に機能します):
<ListView
ItemsSource="{Binding Projects.View}"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedProject, Mode=TwoWay}"
>
ビューモデルProjectsは、実際には、プライベートObservableCollectionによってサポートされるCollectionViewSourceです。ジョシュ・スミスのサンプルプロジェクトからこのアイデアを輝かせたと思いますが、正直なところ今は思い出せません。以下は、xamlバインディングに関連するVMの関連部分です。
private ObservableCollection<ProjectViewModel> _projectsInternal { get; set; }
public CollectionViewSource Projects { get; set; }
private void _populateProjectListings(IEnumerable<Project> openProjects) {
var listing = (from p in openProjects
orderby p.Code.ToString()
select new ProjectViewModel(p)).ToList();
foreach (var pvm in listing)
pvm.PropertyChanged += _onProjectViewModelPropertyChanged;
_projectsInternal = new ObservableCollection<ProjectViewModel>(listing);
Projects = new CollectionViewSource {Source = _projectsInternal};
}
/// <summary>Property that is updated via the binding to the view</summary>
public ProjectViewModel SelectedProject { get; set; }
CollectionViewSourceのFilterプロパティには、バインディングによって正しく取得されるリスト内のビューモデルアイテムのさまざまな述語を返すハンドラーがあります。そのコードの概要は次のとおりです(同じProjectSelctionViewModelにあります):
/// <summary>Trigger filtering of the <see cref="Projects"/> listing.</summary>
public void Filter(bool applyFilter)
{
if (applyFilter)
Projects.Filter += _onFilter;
else
Projects.Filter -= _onFilter;
OnPropertyChanged<ProjectSelectionViewModel>(vm=>vm.Status);
}
private void _onFilter(object sender, FilterEventArgs e)
{
var project = e.Item as ProjectViewModel;
if (project == null) return;
if (!project.IsMatch_Description(DescriptionText)) e.Accepted = false;
if (!project.IsMatch_SequenceNumber(SequenceNumberText)) e.Accepted = false;
if (!project.IsMatch_Prefix(PrefixText)) e.Accepted = false;
if (!project.IsMatch_Midfix(MidfixText)) e.Accepted = false;
if (!project.IsAvailable) e.Accepted = false;
}
ListViewのSelectedItemバインディングはデフォルトで双方向であるため、Mode = TwoWayの設定は冗長ですが、WPFをよりよく理解すると、それについて明示的に気にする必要はありません。
IsSynchronizedWithCurrentItem = Trueを冗長にするコードはどうですか?
これはまともなコードですが、その一部が&quot; magic&quot;を介して機能しているように思えないため、建設的なフィードバックを歓迎します!
乾杯、
ベリル
解決
IsSynchronizedWithCurrentItem
はを同期しますデフォルトの CurrentItem
コントロールの SelectedItem
を持つバインドされたコレクションの CollectionView
。
CollectionView
の CurrentItem
を使用することは決してなく、同じコレクションに2回バインドするように見えないため、問題のプロパティを設定しても、目に見える効果はまったくありません。
プロパティの同期方法のデモ( Kaxaml またはXAMLPadなどのXAMLビューアーの場合):
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<x:Array x:Key="Items" Type="{x:Type sys:String}">
<sys:String>Apple</sys:String>
<sys:String>Orange</sys:String>
<sys:String>Pear</sys:String>
<sys:String>Lime</sys:String>
</x:Array>
</Page.Resources>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<StackPanel Background="Transparent">
<ListBox IsSynchronizedWithCurrentItem="True" ItemsSource="{StaticResource Items}" />
<ListBox IsSynchronizedWithCurrentItem="True" ItemsSource="{StaticResource Items}" />
<!-- This TextBlock binds to the CurrentItem of the Items via the "/" -->
<TextBlock Text="{Binding Source={StaticResource Items}, Path=/}"/>
</StackPanel>
</ScrollViewer>
</Page>