Stelios
Stelios

Reputation: 340

wpf pass datatemplate to new window

i need to send a DataTemplate to a new window for printing purposes.

1) I have create a general Window lets name it PrintPreview that holds the followings:

FlowDocument > BlockUiContainer > ContentControl (Responsible to display the DataTemplate that i will send to it)

The problem is that the bindings inside the datatemplate are not working. (not for all cases )

For example: i have this datatemplate somewhere in my application

<DataTemplate x:Key="MyPrintPreview">
        <DockPanel>
            <TextBlock Text="{Binding SomeProperty1,RelativeSource={RelativeSource AncestorType=UserControl}}"></TextBlock>
            <TextBlock Text="{Binding Source={StaticResource SomeViewModel},Path=SomeProperty2}"></TextBlock>
        </DockPanel>
</DataTemplate>

The above DataTemplate is working very well and show both properties in my current View (UserControl) but when i send this DataTemplate to the New Window PrintPreview i have the following issues

The 1st TextBlock (SomeProperty1) fails to display the content

The 2nd TextBlock (SomeProperty2) show just fine!

i don't know how to make this work. or if am doing it the wrong way?

Upvotes: 0

Views: 373

Answers (1)

mm8
mm8

Reputation: 169220

You should set or bind the Content property of the ContentControl to an object that contains the properties that the elements in the ContentTemplate try to bind to.

So set the ContentTemplate property of the ContentControl to your DataTemplate and set the Content property to the actual object to bind to. That's how a ContentControl is supposed to be used.

Also note that for your first binding to work, the ContentControl must be a child of a UserControl because you are binding to SomeProperty1 of the parent UserControl. If there is no parent UserControl, the binding will always fail.

Upvotes: 1

Related Questions