How can I get the CollectionView that is defined in XAML
You could not just do that?
var _viewSource = this.FindResource("cvs") as CollectionViewSource;
If the data is connected, I assume that will have an updated view.
I tend to just expose the collection view from the VM rather than have the view define it:
public ICollection<Employee> Employees{ get { ... }}public ICollectionView EmployeesView{ get { ... }}
That way your VM has full control over what is exposed to the view. It can, for example, change the sort order in response to some user action.
Found a way, based on J. Lennon's answer. If I pass something that has access to the resources with my command, then I can look up the CollectionViewSource
there.
In XAML (CollectionViewResource
as above):
<Button Command="{Binding Command}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}">Do it!</Button>
And in the VM code:
private void Execute(object parm){ var fe = (FrameworkElement)parm; var cvs = (CollectionViewSource)fe.FindResource("cvs"); cvs.View.Refresh();}
The Execute
is the one that is given to the RelayCommand.
This would answer the question, but I don't like it very much. Opinions?