I can’t figure out what I’m missing here. I want to bind the content of a ContentPresenter to a UIElement. I’m doing something like this:
<Window.Resources>
<DataTemplate x:Key="container">
<Border>
<!--<TextBlock Text="A"/>-->
<ContentPresenter Content="{Binding Element}" />
</Border>
</DataTemplate>
</Window.Resources>
<ContentControl DataContext="{Binding}" ContentTemplate="{StaticResource container}" />
In MainWindow.cs
UIElement Element { get; set; }
public MainWindow()
{
Element = new TextBox() { Text = "A" };
DataContext = this;
InitializeComponent();
}
I can put the textBlock in directly, but when I try the ContentPresenter it does not display anything.
ContentTemplateis a template for content. So, in the case ofContentControl,ContentbecomesDataContextof theDataTemplate. But you can’t setWindowasContentand the property you bind to has to be public.So, after making
Elementpublic property and changing the XAML to:“A” is shown in the window.
I’m assuming this isn’t the real code where you encountered the issue, but doing something like this looks very odd. Maybe you should rethink your design.