I have a Window with two controls: control1 is a control that has a bunch of buttons that execute RoutedCommands; the second control is a TabControl that is bound to a list of documents. The TabControl ContentTemplate is a user control, control2, that has a ListView. I would like to bind the DataContext of control1 to the ListView of control2 of the selected tab. Here is the code:
<Window x:Class="deleteme.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:deleteme"
xmlns:sc="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:s="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<sc:ArrayList x:Key="Tabs">
<local:Document Name="doc1" />
<local:Document Name="doc2" />
<local:Document Name="doc3" />
</sc:ArrayList>
</Window.Resources>
<DockPanel>
<local:control1 DockPanel.Dock="Left" />
<TabControl DockPanel.Dock="Left" ItemsSource="{StaticResource Tabs}" SelectionChanged="TabControl_SelectionChanged">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<local:control2 />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</DockPanel>
</Window>
<UserControl x:Class="deleteme.control1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:deleteme"
x:Name="control">
<StackPanel>
<ItemsControl ItemsSource="{DynamicResource Actions}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="actionButton" Content="{Binding Path=Text}" IsEnabled="{Binding Path=actionButton_IsEnabled}" Command="{Binding Path=Command}" CommandTarget="{Binding ElementName=control, Path=DataContext}" Click="actionButton_Click" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</UserControl>
<UserControl x:Class="deleteme.control2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ScrollViewer>
<ListView x:Name="listView" ItemsSource="{Binding Items}" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
</Grid>
</UserControl>
The reason that I want to bind the ListView of control2 to control1 is twofold: I can use ListView.SelectedItem as the parameter to RoutedCommand.Execute, and I can use the ListView as the CommandTarget so that I can handle actions that pertain to a document in control2.
My problem is that I cannot figure out how to bind the ListView of control2 to control1. Actually, its more likely that I am approaching the problem incorrectly as I'm new to WPF. If anyone has a suggestion, I would appreciate it.