Search code examples
c#xmlwpfxamlresource-files

Resource based on objects in WPF


In a separate project I have a de-serialized xml file with strings that I want to use in my WPF application, these strings are also used within a different project in the same solution, so I can't just move the strings over to the project holding the wpf application.

The program is structured like this:

Project A references B and C

  • WPF application with event handlers

Project B references C

  • GUI logic in F#

Project C

  • XML resource file and de-serializer (written in F#)

Is there a way for me to make a resource or resource dictionary based on the objects from the deserialized xml file? or can I reference the strings stored in the xml file directly?


Solution

  • Turns out I managed to do something that works like I intended, but instead of using the deserialized objects, I used the xml file itself.

    Here's what I did:

    First I changed the XML file's Build Action from Content to Resource by going Right-click the file -> Properties -> Build Action -> Resource The Build Action menu

    Then I went into the Xaml file, and added the following to Window.Resources

    <XmlDataProvider 
        x:Key="DropDownData" 
        Source="/Resource;component/Strings.xml" 
        XPath="/Strings/String" />
    

    In the drop-down menu that I needed the strings I added this:

    ItemsSource="{Binding Source={StaticResource DropDownData}}"
    

    And now my strings are beautifully displayed in the WPF gui.

    Thanks for your suggestions though; they may come in useful in the future.