0
votes

I'm working on a media application and I have a settings window which has a stackpanel with 3 comboboxes in. The ItemSource of these are all bound to the same List(Of SoundDevice) to show the user all of the available sound devices on the computer.

Each of these comboBoxes SelectedValue is bound to a different public property on a sub class (called SoundDevicesClass) of my SettingsClass object allowing the user to choose different devices for different tasks.

I now want to re-use this same stackpanel in another area of the application bound to another instance of my SoundDevicesClass.

My plan is to move the stackpanel into a DataTemplate and then place it in a ContentControl on my settings window. This can then be easily bound to the back end object by

Content="{Binding Settings.SoundDevices}".

The problem I have is what should I bind the comboBoxes ItemsSource to? In the settings window it's bound to a property of the settings window called AllowedSoundDevices using {binding ElementName=SettingsWindow,Path=AllowedSoundDevices} which works fine, but obviously won't work if i move the datatemplate to a shared resource dictionary.

My first thought is to define a new class called something like SoundDevicesSettingsClass such as:

Public Class SoundDevicesSettingsClass
  Public Property AllowedSoundDevices as AllowedSoundDevicesClass
  Public Property SoundDevices as SoundDevicesClass
End Class

And then setting the Content binding to an instance of that class.

Is this the best way or is there a better practice I can follow?

Any and all help is gratefully recieved!

1

1 Answers

0
votes

If ItemsSource refers to equivalent data regardless of what type of view is being displayed, then create additional ViewModel. Place data within and set StackPanel's DataContext to that ViewModel object. No matter where you place StackPanel, it will be independent since its DataContext is already set.