Search code examples
c#wpfcontentpresenter

How to get the data inside `ContentPresenter` in code behind WPF?


is there a way to get the content inside the ContentPresenter in code behind ?

I have this code in my code behind

  ContentPresenter templateParent = GetFrameworkElementByName<ContentPresenter>(listViewItem);

and inside my templateParent have the content as shown below

I want to do something like

String itemName = (ItemName in my templateParent);

how can I do that ? Is it possible to do that ?


Solution

  • I assume Item is the class name for your listviewItem,then you can cast contenetpresenter's content as follows

    String itemName = ((Item)templateParent.Content).ItemName.toString();