IsSynchronizedWithCurrentItem属性和当前项更新

Ber*_*ryl 10 data-binding wpf selecteditem itemssource

我有一个视图模型来管理对话框类型的视图,允许过滤列表(如果需要)和选择项目.无论我是否将IsSynchronizedWithCurrentItem设置为true,代码都能正常工作.我的理解是这个属性在ListView中默认是不正确的,所以我想更好地理解这个属性.

这是视图的xaml中的绑定设置(在没有synch属性设置的情况下也可以正常工作):

    <ListView  
          ItemsSource="{Binding Projects.View}" 
          IsSynchronizedWithCurrentItem="True"
          SelectedItem="{Binding SelectedProject, Mode=TwoWay}"             
                      >
Run Code Online (Sandbox Code Playgroud)

视图模型Projects实际上是一个由私有ObservableCollection支持的CollectionViewSource.我想我从Josh Smith的一个示例项目中琢磨了这个想法,但老实说我现在不记得了.以下是与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; }
Run Code Online (Sandbox Code Playgroud)

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;
    }
Run Code Online (Sandbox Code Playgroud)

设置Mode = TwoWay是多余的,因为默认情况下ListView的SelectedItem绑定是双向的,但我不介意明确它(一旦我更好地理解WPF,我可能会有不同的看法).

我的代码如何使IsSynchronizedWithCurrentItem = True冗余?

我的直觉是,这是一个不错的代码,但我不喜欢它的部分似乎通过"魔术"工作,这意味着我会欢迎任何建设性的反馈!

干杯,
Berryl

H.B*_*.B. 18

IsSynchronizedWithCurrentItem将绑定集合CurrentItem的默认值CollectionViewSelectedItem控件的默认值同步.

由于您从未使用过CurrentItem,CollectionView并且您似乎没有两次绑定到同一个集合,因此设置有问题的属性根本没有可见的效果.


演示属性如何同步(对于像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>
Run Code Online (Sandbox Code Playgroud)