How do I change the binding of a CollectionView.Source?

collectionviewsource xaml
collectionviewsource source
collectionviewsource uwp
collectionviewsource filter
collectionviewsource mvvm
collectionviewsource sortdescriptions
collectionviewsource observablecollection
icollectionview

Initially I had this code in XAML:

<CollectionViewSource x:Name="cSource">
  <CollectionViewSource.Source>
    <Binding Source="{StaticResource NameOfXmlDataProvider}" XPath="letter"/>
  </CollectionViewSource.Source>
<CollectionViewSource>

But I wanted to keep a binding object in the C# code, to be able to dynamically alter it's xpath. Currently I have this code:

CollectionViewSource viewSource = this.FindResource("cSource") as CollectionViewSource;
Binding binding = new Binding( "Source" );
binding.Source = _xmlDataProvider;
binding.XPath = "/new/path/to/nodes";
BindingOperations.SetBinding( viewSource, CollectionViewSource.SourceProperty, binding );

This compiles and doesn't complain but when called it only leads to an empty list. I can't seem to find related examples in the web - most of them deal with the data providers but I want to change the binding.

  • Anybody knows how to fix this?
  • Or is there a better way to do this?
  • Maybe from retrieving the binding object from the collectionview and changing it?

Rather than bind to a static resource in the XAML (yuk) or dynamically change the binding (yukkier), bind to things you can change.

<CollectionViewSource x:Name="cSource">
  <CollectionViewSource.Source>
    <Binding Source="{Binding MyDataProviderProperty}" XPath="{Binding MyDataXPathProperty}"/>
  </CollectionViewSource.Source>
<CollectionViewSource>

If you don't want to go full MVVM, a nice trick is that you can bind your page DataContext to your page's code-behind class by simply naming the UserControl element of your page and use ElementName to bind the datacontext to it (the only restriction is that the DataContext binding can't also be on the UserControl (so place it on the first child, like a grid):

<UserControl x:Class="BindingTests.BindingToSelfExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" x:Name="MyViewClass">
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding ElementName=MyViewClass}">
        <TextBlock Text="{Binding SomePropertyOfTheCodeBehind}" Width="100" Height="20"/>
    </Grid>
</UserControl>

Now, so long as you have notify properties on your code behind called MyDataProviderProperty and MyDataXPath, you can change them to your heart's content.

c#, Rather than bind to a static resource in the XAML (yuk) or dynamically change the binding (yukkier), bind to things you can change. <CollectionViewSource� Then when you need to change the collection view source: cSource = new CollectionViewSource{Source = "newDataSource"}.View; Then if for instance it is a datagrid, you would call: DG.ItemsSource = cSource; DG.Items.Refresh(); Datagrid will now be showing the updated source data.

You need to use the SetBinding() Method of collView. Should be something like collView.SetBinding(CollectionViewSource.SourceProperty, binding)

Look at http://msdn.microsoft.com/en-us/library/ms752347.aspx for further reference.

CollectionViewSource Class (System.Windows.Data), CollectionViewSource enables XAML code to set the commonly used You can think of a collection view as the layer on top of the binding source collection that� Remarks. CollectionViewSource is a proxy for a CollectionView class, or a class derived from CollectionView. CollectionViewSource enables XAML code to set the commonly used CollectionView properties, passing these settings to the underlying view.

The problem with the code from the question is the Source in the binding. So what works is:

 Binding binding = new Binding();

If the Constructor is used with a parameter, the parameter is set as the Path of the binding. The (additional) XPath of the binding is then used from that path. So it tried to find "Source" in the XML which lead to an empty selection. The xpath was then working on an empty set of nodes.

So it is possible to use the bindings from the code.

CollectionViewSource Class (Windows.UI.Xaml.Data), This is particularly useful when you want to bind multiple controls to the same collection and you want the current item in one control to change the current item � When the Grid's selection is changing, I check if the Form is in Edit mode, and if so I cancel the selection change. This works as it should. However, there are some circumstances where the Grid's ItemSource (CollectionViewSource) changes and the Grid looses track of the SelectedItem.

Try this:

ICollectionView cSource {get;set;}

In the constructor:

cSource = new CollectionViewSource{Source = "myDataSource"}.View;

Then when you need to change the collection view source:

cSource = new CollectionViewSource{Source = "newDataSource"}.View;

Then if for instance it is a datagrid, you would call:

DG.ItemsSource = cSource;
DG.Items.Refresh();

Datagrid will now be showing the updated source data.

WPF 4.5 Unleashed, Without this setting, Selector's SelectedItem and the default view's the CollectionViewSource class so that you don't have to change any Binding Paths. I've been working on this Windows Phone 8 app on the side (it's a news app, but mark my words, it's gonna be huge). For the initial development I've been binding to a Pivot to a basic ObservableCollection of type "FeedItem," so basically my XAML was like this.

WPF TaskBoard | Binding to CollectionViewSource, You don't need to set the GroupMemberPath property of the RadTaskBoard when the control's ItemsSource is bound to a CollectionViewSource collection. The� You can specify a binding source by using the Source or the DataContext property, and then provide a blank binding declaration: {Binding}. Scenarios in which this is useful include binding to objects that are of type string, binding to objects with multiple properties you are interested in, or binding to collection objects.

Professional C# 6 and .NET Core 1.0, The value is set to true to change the order of the elements in the user interface. Resources> <CollectionViewSource x:Key="cvs" Source="{Binding}"� Like sorting, filtering in a CollectionViewSource is not automatically done when you change the contents of one of the data bound items. By default, you need to call the Refresh method of the CollectionViewSource. In the example below, we filter on first name “Joan” and then change Joan Fontaine’s first name to “Bob”.

Professional C# 5.0 and .NET 4.5.1, The value is set to true to change the order of the elements in the user interface. Resources> <CollectionViewSource x:Key="cvs" Source="{Binding}"� If you change the contents of one of the bound objects, however, the sorting behavior is not updated. For example, if we change the last name of “Jennifer Jones” to “Garner” and we do not call the Refresh method of the CollectionViewSource, the sorting will be wrong.

Comments
  • There is no SetBinding for CollectionViewSource.
  • I found a way to set the binding using the BindingOperations but this doesn't work. I adapted the question to that.