title: How to use a custom template file to export data
body: <p>I am trying to work out the best way to export data from a WPF application using custom template files, but despite hours of searching have drawn a blank.</p>
  
  <p><strong>The scenario is as follows:</strong></p>
  
  <p>We have a WPF MVVM application for internal company use and one of the Views is for generating invoices. There is an option to Export to a Word document on this View which is currently implemented with various hard coded string literals in the ViewModel, far from ideal.</p>
  
  <p>The current setup does work, but doesn't feel right to me and I'd like to improve it. We also have a new requirement to add different types of invoice, so I'm taking this opportunity to tidy things up.</p>
  
  <p>The export process currently involves creating an HTML document as a string and then inserting this into a Word document. There may be better ways of doing this, but that is out of the scope of this question, the same question would apply if the final output required was HTML. To keep things simple, let's just say that we're interested in any type of export file that can be represented as a string and with no dynamic layout such as multiple rows of data i.e. we're just doing a find/replace on the string.</p>
  
  <p><strong>What I would like to do:</strong></p>
  
  <ul>
  <li>Create custom template files containing placeholders</li>
  <li>Specify in the View which template(s) to use</li>
  <li>Inject data into the template(s) using data in the ViewModel</li>
  </ul>
  
  <p>Is this the right way to be going about this? If not, what would be a better alternative?</p>
  
  <p>Where should the template file(s) be stored? I have assumed they would be resources in the WPF application.</p>
  
  <p>Where should the template manipulation / data injection be done? I initially assumed a string would be loaded into the ViewModel and this would do the manipulation, but maybe there is some better way of manipulating the data in the View.</p>
  
  <p>After all that, let's get on with the actual question!</p>
  
  <p><strong>The question:</strong></p>
  
  <p>For any given export file type that can be represented as a string, what is the best way to store a template in WPF MVVM and inject data into it?</p>