Reputation: 660
In my XAML code I have defined a CollectionViewSource as a grid resource:
<Grid.Resources>
<CollectionViewSource x:Key="AgentsViewSource"
Source="{StaticResource Agents}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="ID" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Grid.Resources>
Further down as a child of a toolbox which again is a child of the grid I have a ComboBox which should change the SortDescription
<ComboBox Name="SortOrderBox" ItemsSource="{Binding AvailableProperties}" SelectionChanged="newSortOrder_OnSelectionChanged" Width="130" FontSize="15"/>
With the following code behind:
private void newSortOrder_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var newSortOrder = (string) SortOrderBox.SelectedItem;
var sortDescription = new SortDescription(newSortOrder, ListSortDirection.Descending);
var source = (CollectionViewSource) FindResource("AgentsViewSource");
source.SortDescriptions.Clear();
source.SortDescriptions.Add(sortDescription);
}
However, when I try to use FindResource I always get an exception. I really have no idea what I'm doing wrong, so all input is appreciated.
Upvotes: 0
Views: 645
Reputation: 81253
You are trying to find resource under window/userControl resources.
Set x:Name
on grid and call FindResource on grid instance because it is declared as a resource under grid and not under window/UserControl.
<Grid x:Name="grid">
.....
</Grid>
Code behind:
grid.FindResource("AgentViewSource")
Upvotes: 2