HighTechTalks DotNet Forums  

WPF ComboBox Binding Confusion

Dotnet Framework (WinForms DataBinding) microsoft.public.dotnet.framework.windowsforms.databinding


Discuss WPF ComboBox Binding Confusion in the Dotnet Framework (WinForms DataBinding) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
ryan baldwin
 
Posts: n/a

Default WPF ComboBox Binding Confusion - 06-20-2008 , 12:29 PM






I'm creating a sample project to help solve another problem (oh how this
rabbit hole is getting deep!) and I'm having the following problem.

Given the following XmlDataProvider:

<Window.Resources>
<XmlNamespaceMappingCollection x:Key="namespaceMappings">
<XmlNamespaceMapping Uri="http://www.w3.org/2001/XMLSchema"
Prefix="xs" />
<XmlNamespaceMapping Uri="http://www.mssample.com" Prefix="ui" />
</XmlNamespaceMappingCollection>

<XmlDataProvider x:Key="TaxonomyDataProvider" Source="Definitions.xsd"
XmlNamespaceManager="{StaticResource
namespaceMappings}"
XPath="/xs:schema/xs:complexType" />
</Window.Resources>

I'm trying to bind a ComboBox to the above XmlDataProvider with the
following:

<ComboBox Grid.Row="0" x:Name="cboAssetGroups" MinWidth="75"
IsSynchronizedWithCurrentItem="True" SelectedIndex="-1"
ItemsSource="{Binding Source={StaticResource
TaxonomyDataProvider}}"
DisplayMemberPath="@ui:label"
SelectedValuePath="@name">

Everything works fine - I can see the list of combobox items and they
are indeed being mapped to the @name attribute. However, when I select
a value from the combobox, the selected item is blank, and the output
produces the following error:

System.Windows.Data Error: 40 : XML binding failed. Cannot obtain result
node collection because of bad source node or bad Path.;
SourceNode='xs:complexType'; Path='@ui:label'
BindingExpression:Path=/InnerText; DataItem='XmlElement'
(HashCode=64083652); target element is 'ComboBox'
(Name='cboAssetGroups'); target property is 'NoTarget' (type 'Object')
XPathException:'System.Xml.XPath.XPathException: Namespace Manager or
XsltContext needed. This query has a prefix, variable, or user-defined
function.
at MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryT ree()
at System.Xml.XPath.XPathNavigator.Evaluate(XPathExpr ession expr,
XPathNodeIterator context)
at System.Xml.XPath.XPathNavigator.Evaluate(XPathExpr ession expr)
at System.Xml.XPath.XPathNavigator.Select(XPathExpres sion expr)
at System.Xml.XPath.XPathNavigator.Select(String xpath)
at System.Xml.XmlNode.SelectNodes(String xpath)
at MS.Internal.Data.XmlBindingWorker.SelectNodes()'

Why? I already have an XmlNamespaceMapping for the @ui namespace... I
don't understand why I can see the items but selecting them throws an error.

Here is the xml sourcefile in question (it's an xsd):
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Definitions"
targetNamespace="http://tempuri.org/Definitions.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/Definitions.xsd"
xmlns:mstns="http://tempuri.org/Definitions.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ui="http://www.mssample.com"
Quote:
xs:complexType name="Cat" ui:label="Cat"
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="0" name="Name"
type="xs:string" ui:label="Name" ui:maxLength="30" />
<xs:element maxOccurs="1" minOccurs="0" name="Colour"
type="xs:string" ui:allowCustom="true" ui:label="Colour"
ui:maxLength="20" uiption="/choices/attribute[@key='Colour']/option" />
<xs:element maxOccurs="1" minOccurs="0" name="Length"
ui:label="Length">
<xs:complexType>
<xs:attribute name="value" type="xs:decimal" use="required" />
<xs:attribute name="unit" type="xs:string"
ui:allowCustom="true"
uiptions="/choices/attribute[@key='Length']/option" />
</xs:complexType>
</xs:element>
<xs:element maxOccurs="1" minOccurs="0" name="Weight"
ui:label="Weight">
<xs:complexType>
<xs:attribute name="value" type="xs:decimal" use="required" />
<xs:attribute name="unit" type="xs:string"
ui:allowCustom="true"
uiptions="/choices/attribute[@key='Length']/option" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Dog" ui:label="Cat">
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="0" name="Name"
type="xs:string" ui:label="Name" ui:maxLength="30" />
<xs:element maxOccurs="1" minOccurs="0" name="Colour"
type="xs:string" ui:allowCustom="true" ui:label="Colour"
ui:maxLength="20" uiption="/choices/attribute[@key='Colour']/option" />
<xs:element maxOccurs="1" minOccurs="0" name="Length"
ui:label="Length">
<xs:complexType>
<xs:attribute name="value" type="xs:decimal" use="required" />
<xs:attribute name="unit" type="xs:string"
ui:allowCustom="true"
uiptions="/choices/attribute[@key='Length']/option" />
</xs:complexType>
</xs:element>
<xs:element maxOccurs="1" minOccurs="0" name="Weight"
ui:label="Weight">
<xs:complexType>
<xs:attribute name="value" type="xs:decimal" use="required" />
<xs:attribute name="unit" type="xs:string"
ui:allowCustom="true"
uiptions="/choices/attribute[@key='Weight']/option" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Programmer" ui:label="Programmer">
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="0" name="Name"
type="xs:string" ui:label="Name" ui:maxLength="30" />
<xs:element maxOccurs="1" minOccurs="0" name="Height"
ui:label="Height">
<xs:complexType>
<xs:attribute name="value" type="xs:decimal" use="required" />
<xs:attribute name="unit" type="xs:string"
ui:allowCustom="true"
uiptions="/choices/attribute[@key='Height']/option" />
</xs:complexType>
</xs:element>
<xs:element maxOccurs="1" minOccurs="0" name="Weight"
ui:label="Weight">
<xs:complexType>
<xs:attribute name="value" type="xs:decimal" use="required" />
<xs:attribute name="unit" type="xs:string"
ui:allowCustom="true"
uiptions="/choices/attribute[@key='Weight']/option" />
</xs:complexType>
</xs:element>
<xs:element maxOccurs="1" minOccurs="0" name="IsBald"
ui:label="Is Bald" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
</xs:schema>


Reply With Quote
  #2  
Old   
Linda Liu[MSFT]
 
Posts: n/a

Default RE: WPF ComboBox Binding Confusion - 06-23-2008 , 06:43 AM






Hi Ryan,

I performed a test based on your sample code and did reproduce the problem
on my side.

If I bind the XML data source provider to a ListBox(define a TextBlock in
the DataTemplate for the ListBox's ItemTemplate and bind the label
attribute to the Text property of the TextBlock), it works well without
any errors. The XAML code is a follows:

<ListBox ItemsSource="{Binding Source={StaticResource
TaxonomyDataProvider}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding XPath=@ui:label}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

So it seems that this is an issue about the ComboBox control. I will
consult this issue in our internal discussion group. As soon as I get any
news, I will update it to you.

I appreciate your patience!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #3  
Old   
Linda Liu[MSFT]
 
Posts: n/a

Default RE: WPF ComboBox Binding Confusion - 06-23-2008 , 06:43 AM



Hi Ryan,

I performed a test based on your sample code and did reproduce the problem
on my side.

If I bind the XML data source provider to a ListBox(define a TextBlock in
the DataTemplate for the ListBox's ItemTemplate and bind the label
attribute to the Text property of the TextBlock), it works well without
any errors. The XAML code is a follows:

<ListBox ItemsSource="{Binding Source={StaticResource
TaxonomyDataProvider}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding XPath=@ui:label}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

So it seems that this is an issue about the ComboBox control. I will
consult this issue in our internal discussion group. As soon as I get any
news, I will update it to you.

I appreciate your patience!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #4  
Old   
Linda Liu[MSFT]
 
Posts: n/a

Default RE: WPF ComboBox Binding Confusion - 06-24-2008 , 01:20 AM



Hi Ryan,

We have reported this issue to our product team and this issue is confirmed
a limitation of the ComboBox control in WPF.

The reason behind this issue is that ComboBox uses a private binding to get
the text representing the selected item (see TextSearch.GetPrimaryText).
This binding binds directly to the selected item, in this case an
XmlElement. There's no back link from the XmlElement to the
XmlDataProvider, and therefore no way to get to the namespace map. So any
binding that uses ns prefixes won't be able to map them to Uri's.

A workaround is to specify Binding.XmlNamespaceManager for the ComboBox
explicitly:

<ComboBox Binding.XmlNamespaceManager="{StaticResource namespaceMappings}"
.../>

This workaround works perfectly on my side. Please try it on your side to
see if the problem is fixed.

Thank you for reporting this problem to us! It will definitely improve our
product.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #5  
Old   
Linda Liu[MSFT]
 
Posts: n/a

Default RE: WPF ComboBox Binding Confusion - 06-24-2008 , 01:20 AM



Hi Ryan,

We have reported this issue to our product team and this issue is confirmed
a limitation of the ComboBox control in WPF.

The reason behind this issue is that ComboBox uses a private binding to get
the text representing the selected item (see TextSearch.GetPrimaryText).
This binding binds directly to the selected item, in this case an
XmlElement. There's no back link from the XmlElement to the
XmlDataProvider, and therefore no way to get to the namespace map. So any
binding that uses ns prefixes won't be able to map them to Uri's.

A workaround is to specify Binding.XmlNamespaceManager for the ComboBox
explicitly:

<ComboBox Binding.XmlNamespaceManager="{StaticResource namespaceMappings}"
.../>

This workaround works perfectly on my side. Please try it on your side to
see if the problem is fixed.

Thank you for reporting this problem to us! It will definitely improve our
product.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.