wpf - IsSynchronizedWithCurrentItem 属性和当前项目更新

标签 wpf data-binding selecteditem itemssource

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

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

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

View 模型 Projects 实际上是由私有(private) ObservableCollection 支持的 CollectionViewSource。我想我是从 Josh Smith 的一个示例项目中得出这个想法的,但老实说,我现在不记得了。这是与 xaml 绑定(bind)相关的 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 属性有一个处理程序,该处理程序返回列表中 View 模型项的各种谓词,这些谓词被绑定(bind)正确拾取。这是该代码的要点(在同一个 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 绑定(bind)是两种方式,但我不介意明确说明(一旦我更好地理解 WPF,我可能会有不同的感觉)。

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

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

干杯,
绿柱石

最佳答案

IsSynchronizedWithCurrentItem同步 CurrentItem 默认的 CollectionView SelectedItem 绑定(bind)的集合你的控制。
由于您从不使用 CurrentItemCollectionView并且您似乎没有两次绑定(bind)到同一个集合,设置有问题的属性根本没有明显的效果。

属性如何同步的演示(适用于 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>

关于wpf - IsSynchronizedWithCurrentItem 属性和当前项目更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1822421/

相关文章:

c# - 在 WPF 中折叠网格

c# - mvvm 绑定(bind) selecteditem 以更新 ListView

wpf - 您如何以编程方式将焦点设置到已具有焦点的 WPF ListBox 中的 SelectedItem?

c# - WPF 工具包 PropertyGrid ExpandableObject 属性未正确显示嵌套类

c# - 在 Windows 8 Metro App 中对 GridView 进行分组

使用数据绑定(bind) PowerBI 的 Highcharts 自定义视觉效果

Android:在 AlertDialog 内的多选 ListView 中选择项目

wpf - 具有动态集合属性的 ViewModel

.net - DataTemplate 作为具有可定制绑定(bind)的资源