locked
Binding control property to embedded file resource? RRS feed

  • Question

  • I have an xml file that I have added to my project as an embedded resource (see image below).    I have created a custom control that has a dependency property called Keyboard, of type KeyboardConfiguration.  The XML file is serialized data of an instance of KeyboardConfiguration.    What I would like to do is bind the XML file resource to the Keyboard property of my control, and using a Converter class, deseralize the XML to the class instance.  Something like this:

     

                <Window.Resources>
                    <lib:ResourceXmlToKeyboardConfigurationConverter x:Key="converter" />
    
                </Window.Resources>
                <VirtualKeyboard      
                    Grid.Column="1" 
                    x:Name="vKeyboard"
                    AttachedEditor="{Binding ElementName=editField}" 
                    UseAttachedEditor="True"             
                    Keyboard="{Binding Source={StaticResource Resources}, Path=DefaultEnglishLayout.xml, Converter={StaticResource converter}}"
                    />
    


     



     

     

    However, as you might have guessed, this doesn't work.

    I've tried various combinations and so far, no luck.  I'm not sure this is even possible

    Any thoughts?

    Matt

     

     


    • Edited by tatman blue Thursday, December 22, 2011 5:04 PM
    Thursday, December 22, 2011 5:03 PM

Answers

All replies

  • Hi tatman blue,

    Based on my understanding, I think what you need is XmlDataProvider, you could use this class to provide xml data:

    http://msdn.microsoft.com/en-us/library/system.windows.data.xmldataprovider.aspx

    you could set source property, and then use it in your app.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by tatman blue Wednesday, January 4, 2012 4:17 PM
    Friday, December 23, 2011 6:11 AM
  • Hi tatman blue,

    How about your issue.

     

    best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 4, 2012 6:28 AM
  • At first I was not able to get XmlDataProvider to work.   After some research, I figured out how to get my converter to create a class object instance from the XML data provided by XMLDataProvider.   The key bit of code is this:

     

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (null == value)
                    return null;
    
                System.Collections.ICollection xmlCollection = value as System.Collections.ICollection;
                System.Xml.XmlElement xmlElement = null;
    
                foreach (object something in xmlCollection)
                {
                    xmlElement = something as System.Xml.XmlElement;
                    break;
                }
    
    ...

     

    I'm sure this code could be optimized with Linq.   I also had to change the XML file to Resource vs Embedded resource.

    Anyways, yes it works.  Thank you.  

     


    • Edited by tatman blue Wednesday, January 4, 2012 4:23 PM
    Wednesday, January 4, 2012 3:13 PM