I have a TabControl
which I’m trying to hook up to a collection of ViewModel
s with ReactiveUI. The ViewModels are various subclasses of a particular base class, and there are different Views defined for the different subclasses, which are resolved by a ViewModelViewHost
in the TabControl.ContentTemplate
.
Here’s the relevant snippet from the View
:
<TabControl Name="MyTabControl" SelectedIndex="0">
<TabControl.ItemTemplate>
<DataTemplate>
<!--
MyTabControl.ItemsSource is set at runtime via Reactive binding in a this.WhenActivated() block,
so the bindings in this ItemTemplate generate design-time resolution errors which resolve ok at runtime.
I'd prefer not to have false-positive designer resolution errors.
-->
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" Margin="2" />
<CheckBox VerticalAlignment="Center"
IsChecked="{Binding Path=Complete, Mode=OneWay}"
IsEnabled="{Binding Path=Ready, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<rxui:ViewModelViewHost ViewModel="{Binding}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
The constructor of the ViewModel
looks like this:
public MyView()
{
InitializeComponent();
ViewModel = new MyViewModel();
this.WhenActivated(disposer =>
{
this.OneWayBind(ViewModel,
vm => vm.Steps,
v => v.MyTabControl.ItemsSource)
.DisposeWith(disposer);
});
}
The <rxUI:ViewModelViewHost />
element works nicely for the TabControl.ContentTemplate
. I’d like to update the XAML so that the TabControl.ItemTemplate
is also defined via ReactiveUI bindings rather than using WPF bindings to the ViewModel’s properties, because WPF bindings to Reactive ViewModels tend to leak memory and because I don’t like to have bindings in my XAML with design-time resolution failures.
Is there some way to use ViewModelViewHost
that would resolve a different UserControl in the TabControl.ItemTemplate
context vs the TabControl.DataTemplate
context? Resolving based entirely on the TabControl’s item doesn’t work very well in this case since I’m trying to contextually resolve two different views for the exact same TabItem.