Basically I want to convert something I did in xaml to C#. This is related to the following issue: Bind a string in xaml to a property
Here is why the proxy is needed and used in my case: http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
Proxy class to allow me to bind to the CollectionContainer:
public class BindingProxy : Freezable
{
#region Overrides of Freezable
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
#endregion
public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
// Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
Proxy defined in my control's resources:
<UserControl.Resources>
<global:BindingProxy x:Key="proxy" Data="{Binding }" />
</UserControl.Resources>
Xaml code that I ultimately want to convert to C#:
<ComboBox ItemsSource="{Binding NameCollection}">
<ComboBox.ItemsSource>
<CompositeCollection>
<x:StaticExtension Member="VM:NameClass.NoName " />
<CollectionContainer Collection="{Binding Data.NameCollection, Source={StaticResource proxy}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
String Constant that I don't want in my View Model collection, but want shown to the user:
public class NameClass
{
public const string NoName = "[None]";
}
I think I was over complicating it, by trying to include the proxy. But really it was a pretty simple solution:
ComboBox comboBox1 = new ComboBox { Height = 18, Width = 100, FontSize = 9.5 };
CompositeCollection compositeCollection = new CompositeCollection();
compositeCollection.Add(NameClass.NoName);
CollectionContainer collectionContainer = new CollectionContainer();
collectionContainer.Collection = ItemsSource1;
compositeCollection.Add(collectionContainer);
comboBox1.ItemsSource = compositeCollection;