2
votes

I have a usercontrol (ItemsView) that I use in one of my other view. Since I need to access its ViewModel, the ItemsViewViewModel is contained by the ViewModel of the view that contained the control. I use this control at many times and I find it useful to bind a collection on the ItemsSource of my ItemsView control (differently, depending on the view that used it). It works, but not all the time. Here some code :

<local:ItemsView DataContext="{Binding Path=ItemsViewModel}" ItemsSource="{Binding Path=DataContext.CurrentItem.Children, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />

The problem is that the DataContext of the ItemsView is sometimes set before the ItemsSource, and sometimes after. This is problematic because the ItemsSource is a dependency property linked to the ItemsViewViewModel. Is there a way to have the DataContext set before the ItemsSource everytime?

1

1 Answers

0
votes

After InitializeComponent, set SelectedIndex to -1 - this worked for me.