使用CollectionViewSource绑定

我正在尝试使用CollectionViewSource实现一些combobox排序。 此combobox实际上是数据模板的一部分,并在列表视图中重复。 我的第一种方法似乎有效(使用CollectionViewSource)但我的所有combobox都共享相同的数据上下文。 这使得每当其他一个盒子被改变时,所有其他盒子都改变以反映 – 而不是期望的副作用。

我决定退回并尝试使用内联xaml实现一个基本的combobox(不在数据模板中)来指定CollectionViewSource(而不是将cvs创建为静态资源)。 我无法成功显示数据。 因为我还是WPF的新手,所以我可能会完全错误。

这是我的combobox的xaml:

             

此combobox所在的用户控件的DataContext绑定到一个对象,该对象具有名为Configurations的ObservableCollection,并且每个配置都有一个名为AgencyName的属性。 我已经证实使用没有cvs的标准绑定可以正常工作,所以我知道在那个协议中一切都很好。

任何帮助都会非常感激,因为我已经找不到我的老板的借口:)。 我也不想下载代码并在后面的代码中进行排序(我可以在构建ObservableCollection时使用,但是违反DRY原则的恕我直言)。

究竟是什么意思“每当其他一个盒子被改变时,所有其他盒子都改变了以反映”? 你在谈论SelectedItem吗? 如果是这样,那么它可能有助于在ComboBox中设置IsSynchronizedWithCurrentItem = false

除此之外:我认为只要你在后面的代码中创建和排序你的ICollectionView,就不会违反DRY原则,因为你在XAML中不需要做更多的事情。 但是我看到可能有其他理由说应该在视图中完成像排序这样的function,用Model-View-ViewModel来讲。

没有阅读您的整个post,但问题是默认情况下共享资源。 因此,每个combobox都引用相同的集合视图。 集合视图包括跟踪选择,因此更改一个combobox中的选择将影响其他组合。

您可以阻止它被共享,而不是将CVS移动到本地资源:

  

虽然可能为时已晚,但我会将此答案留给可能遇到此问题的其他人。 您对CollectionViewSource.Source的绑定不起作用,因为CollectionViewSource不属于可视/逻辑树,它既不inheritance数据上下文也不能引用ComboBox作为绑定源。 我能够使用以下类以一种丑陋而简单的方式解决这个问题:

 ///  /// Provides a way to set binding between a control /// and an object which is not part of the visual tree. ///  ///  /// A bright example when you need this class is having an ///  bound to a . /// The tricky thing arises when you want the  /// to be bound to some property of the  /// (eg to its data context, and to the view model). Since ///  doesn't belong to the visual/logical tree, /// its not able to reference the . To stay in markup, /// you do the following: /// 1) Add an instance of the  to the resources /// of some parent element; /// 2) On the  set the  attached property to the /// instance created on step 1) using ; /// 3) Set the  to a binding which has /// source set (via ) to  /// and path set to the  (which will be the control /// on which you set the attached property on step 2) plus the property of interest /// (eg ): ///  ///  /// . /// /// So the result is that when assigning the attached property on a control, the assigned ///  stores the reference to the control. And that reference can be /// retrieved from the . ///  public sealed class BindingBridge : DependencyObject { #region BridgeInstance property public static BindingBridge GetBridgeInstance(DependencyObject obj) { Contract.Requires(obj != null); return (BindingBridge)obj.GetValue(BridgeInstanceProperty); } public static void SetBridgeInstance(DependencyObject obj, BindingBridge value) { Contract.Requires(obj != null); obj.SetValue(BridgeInstanceProperty, value); } // Using a DependencyProperty as the backing store for BridgeInstance. This enables animation, styling, binding, etc... public static readonly DependencyProperty BridgeInstanceProperty = DependencyProperty.RegisterAttached("BridgeInstance", typeof(BindingBridge), typeof(BindingBridge), new PropertyMetadata(OnBridgeInstancePropertyChanged)); #endregion BridgeInstance property #region SourceElement property public FrameworkElement SourceElement { get { return (FrameworkElement)GetValue(SourceElementProperty); } private set { SetValue(SourceElementPropertyKey, value); } } // Using a DependencyProperty as the backing store for SourceElement. This enables animation, styling, binding, etc... private static readonly DependencyPropertyKey SourceElementPropertyKey = DependencyProperty.RegisterReadOnly("SourceElement", typeof(FrameworkElement), typeof(BindingBridge), new PropertyMetadata(null)); public static readonly DependencyProperty SourceElementProperty; #endregion SourceElement property ///  /// Initializes the  class. ///  static BindingBridge() { SourceElementProperty = SourceElementPropertyKey.DependencyProperty; } private static void OnBridgeInstancePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var sourceElement = (FrameworkElement)d; var bridge = (BindingBridge)e.NewValue; bridge.SourceElement = sourceElement; } } 

以下是使用示例(未显示资源字典):

               

绑定取决于VisualTree,cvs不是可视化的,因此Binding不起作用。

您可以改用x:Reference。