11

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

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

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

视图模型 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; }

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

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

我的代码如何使 IsSynchronizedWithCurrentItem=True 变得多余?

我的直觉是,这是不错的代码,但我不喜欢它似乎是通过“魔法”工作的,这意味着我欢迎任何建设性的反馈!

干杯,
贝里尔

4

1 回答 1

21

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

由于您从不使用CurrentItemofCollectionView并且您似乎没有两次绑定到同一个集合,因此设置有问题的属性根本没有明显的效果。


属性如何同步的演示(适用于 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>
于 2011-06-14T01:06:56.613 回答