Question

I have a couple specific user controls to Show some Content, e.g. simple like Image, WebControl but also two complex specific custom controls drawing on a canvas.

Now I thought using the DataTemplateSelector to handle the different UserControls. I actully used this http://tech.pro/tutorial/807/wpf-tutorial-how-to-use-a-datatemplateselector as a reference.

I changed the code so the form loads the UserControls dynamically (according to the file extension) in the following collection:

ObservableCollection<string> _pathCollection = new ObservableCollection<string>();

The only difference to the reference is now I want to navigate back and forward to the next control by showing one control only at the time. Which control should I use instead of ListView?

<Grid>
    <ListView ScrollViewer.CanContentScroll="False" 
              ItemsSource="{Binding ElementName=This, Path=PathCollection}" 
              ItemTemplateSelector="{StaticResource imgStringTemplateSelector}">
    </ListView>
 </Grid>

How do I need to bind it to the template (equal to ItemTemplateSelector above)? WPF is still very new to me and I am learning.

Was it helpful?

Solution

Use a ContentControl. Bind your current item to the Content-property and the DataTemplateSelector to the ContentTemplateSelector-property.

<ContentControl Content="{Binding Path=CurrentItem, Mode=OneWay}", ContentTemplateSelector="{StaticResource imgStringTemplateSelector}" />

Your CurrentItem should be a DependencyProperty or a INotifyPropertyChanged-property of your DataContext. When you change your CurrentItem, the ContentControl will update the template automatically with help of your TemplateSelector.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top