I want to create a TabControl with a number of \"static\" TabItems (explicitly typed in XAML) and a number of dynamically added TabItems. To achieve this I tried to use a Co
I had a similar scenario. To solve this I found the following article.
This post explains how to create a freezable proxy object that you can set your data context to. You then add this proxy as a resource that can be referenced as a static resource. See the following:
public class BindingProxy : Freezable
{
public static DependencyProperty DataContextProperty = DependencyProperty.Register(
"DataContext", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
public object DataContext
{
get { return GetValue(DataContextProperty); }
set { SetValue(DataContextProperty, value); }
}
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
}
This can then be used in your xaml like this:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:y="clr-namespace:Namespace.Of.BindingProxy"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
>
<Window.Resources>
<x:Array x:Key="SomeTexts" x:Type="sys:String">
<sys:String>Text1</sys:String>
<sys:String>Text2</sys:String>
</x:Array>
<y:BindingProxy x:Key="Proxy" DataContext="{Binding}" />
</Window.Resources>
<TabControl>
<TabControl.ItemsSource>
<CompositeCollection>
<TabItem Header="Test">
<StackPanel>
<TextBlock x:Name="MyText" Text="Blah" />
<TextBlock Text="{Binding DataContext.SomeProperty, Source={StaticResource Proxy}}" />
</StackPanel>
</TabItem>
<CollectionContainer Collection="{StaticResource SomeTexts}" />
</CompositeCollection>
</TabControl.ItemsSource>
</TabControl>
</Window>