Sladescross's Blog

Blogging about Sharepoint related stuff

WPF XML Binding and Namespaces September 30, 2011

Filed under: Local-Name,WPF,XMLNameSpaceManager — sladescross @ 11:37 am

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cb6b22e7-3a48-425b-bbad-2575d2618968/

I suppose after looking at the code, you should be able to understand why it works after specify the Binding.XmlNamespaceManager attached property for TextBlock.

ListBox is data bound to a data provider which has xml namespace mapping information, but the binding on the TextBlock doesn’t have this information, that’s why it fails.

Actually, when doing master detail data binding, it’s more appropriate to do something like the following:

<Page xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
      xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”&gt;
    <Page.Resources>
        <XmlNamespaceMappingCollection x:Key=”namespaceMappings”>
            <XmlNamespaceMapping Uri=”http://www.footballism.com/2005/SoccerPlayers&#8221; Prefix=”p”/>
        </XmlNamespaceMappingCollection>
       
        <XmlDataProvider x:Key=”dataProvider”
                         XmlNamespaceManager=”{StaticResource namespaceMappings}”
                         XPath=”p:players/p:player”>
            <x:XData>
                <p:players xmlns:p=”http://www.footballism.com/2005/SoccerPlayers”&gt;
                    <p:player>
                        <p:fullName>Sebastian Batistuta</p:fullName>
                        <p:age>26</p:age>
                    </p:player>
                    <p:player>
                        <p:fullName>Andriey Shevchenko</p:fullName>
                        <p:age>30</p:age>
                    </p:player>
                    <p:player>
                        <p:fullName>Paviel Nedved</p:fullName>
                        <p:age>21</p:age>
                    </p:player>
                    <p:player>
                        <p:fullName>David Beckham</p:fullName>
                        <p:age>19</p:age>
                    </p:player>
                </p:players>
            </x:XData>
        </XmlDataProvider>
    </Page.Resources>
    <StackPanel DataContext=”{Binding Source={StaticResource dataProvider}}”>
        <TextBlock
            Text=”{Binding XPath=p:fullName}”
            FontWeight=”Bold”/>
        <ListBox ItemsSource=”{Binding}”
                 x:Name=”listBox”
                 DisplayMemberPath=”p:fullName”
                 IsSynchronizedWithCurrentItem=”True”>
        </ListBox>
    </StackPanel>
</Page>

http://stackoverflow.com/questions/190102/xml-data-binding-with-namespaces

<TextBox Name=”nameText”> 
    <TextBox.Text> 
       <Binding XPath=”*[local-name()='Name']” /> 
    </TextBox.Text> 
 </TextBox>

About these ads
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 63 other followers