views:

146

answers:

1

This is very similar to this question I asked earlier. I am hoping to be clearer and get a different answser.

I have a Data Object (called MockUI). It has a data template (in app.xaml) like this:

<DataTemplate DataType="{x:Type local:MockWI}">
    <Button Content="{Binding Name}"/>
</DataTemplate>

In my code I want create a UI object that what the data template is. So I have myMockWI and I want to find out what template that would use and get the object it creates (in this case a button with the content set to myMockWI).

I have tried to just make a button:

Button myButton = new Button {Content = myMockWI}

but as you can probably guess, that creates the button then puts another button inside that button (because the data template is applied). How can I get one button only?

A: 

Turns out that I just needed to go up the UI tree a bit.

If I make a new ContentControl then it has no Look to it and takes on whatever the datatemplate is.

So my code changes from above to this:

ContentControl myControl = new ContentControl {Content = myMockWI};
Vaccano
You are correct in doing so. Anything that derives from ContentControl may also use the DataTemplate with DataType set to host the data object, eg. ContentPresenter, ListBox, ListView, etc.
Tri Q