IsSynchronizedWithCurrentItem属性和当前项更新 [英] IsSynchronizedWithCurrentItem attribute and current item updates

查看:161
本文介绍了IsSynchronizedWithCurrentItem属性和当前项更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a view model to manage a dialog type of view that allows filtering of a listing (if necessary) and selection of an item. The code works fine whether I set IsSynchronizedWithCurrentItem to true or not. My understanding is that this property is not true by default in a ListView, so I'd like to better understand this property.

这是视图的xaml中的绑定设置(没有同步属性设置的工作原理):

Here is the binding setup in the view's xaml (which works just as well without the synch property setting):

    <ListView  
          ItemsSource="{Binding Projects.View}" 
          IsSynchronizedWithCurrentItem="True"
          SelectedItem="{Binding SelectedProject, Mode=TwoWay}"             
                      >

视图模型项目实际上是由私有的ObservableCollection支持的CollectionViewSource。我想我从Josh Smith的一个示例项目中吸取了这个想法,但是我真的不记得了。以下是与xaml绑定相关的VM的相关部分:

The view model Projects is actually a CollectionViewSource that is backed by a private ObservableCollection. I think I glommed this idea from a sample project of Josh Smith's, but I honestly don't recall right now. Here is the relevant portion of the VM that relates to the xaml binding:

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中):

The Filter property of the CollectionViewSource has a handler which returns various predicates on the view model items in the list which is picked up by the bindings correctly. Here is the gist of that code (which is in the same 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;
    }

设置Mode = TwoWay是冗余的,因为ListView的SelectedItem绑定是两种方式默认情况下,但我不介意明确表达(我可能会感觉不同,一旦我理解WPF更好)。

Setting the Mode=TwoWay is redundant since the ListView's SelectedItem binding is two way by default, but I don't mind being explicit about it (I might feel differently about that once I understand WPF better).

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

What about my code is making the IsSynchronizedWithCurrentItem=True redundant?

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

My gut is that this is decent code, but I don't like that pieces of it seem to be working via "magic", which means I would welcome any constructive feedback!

干杯,

Berryl

Cheers,
Berryl

推荐答案

IsSynchronizedWithCurrentItem 同步 CurrentItem system.componentmodel.icollectionview.aspxrel =noreferrer> CollectionView 的bou您的控件的 SelectedItem

由于您从不使用 CurrentItem CollectionView ,而您似乎并不会绑定到相同的集合两次设置相关属性完全没有可见的影响。

Since you never use the CurrentItem of the CollectionView and you do not appear to bind to the same collection twice setting the property in question has no visible effect at all.

演示如何同步属性(对于XAML观众,如 Kaxaml 或XAMLPad):

Demo of how the property syncs (for XAML viewers like Kaxaml or XAMLPad):

<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>

这篇关于IsSynchronizedWithCurrentItem属性和当前项更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆