wpf中,xml绑定到datacontext和绑定到普通object 有何区别?

1.XML如下:

代码
   
     
< Root >
< Unions >
< Item FURN_GRP_ID ="64" FURN_GRP_NAME ="ddd" />
< Item FURN_GRP_ID ="61" FURN_GRP_NAME ="沿海地" />
< Item FURN_GRP_ID ="62" FURN_GRP_NAME ="地中海" />
< Item FURN_GRP_ID ="63" FURN_GRP_NAME ="沙滩" />
</ Unions >
</ Root >

 

 

 

2. Xaml里面这样子绑定
<XmlDataProvider x:Key="typeData" Source="Xml/Base.xml" XPath="/Root" />

3.把指定的节点绑定到DataContext和Test
<UC:UCUnionList Grid.Column="3" Margin="27,0,83,10" x:Name="ucUnionList"
                                 DataContext="{Binding Source={StaticResource typeData},XPath=/Root/Unions/Item}"
                                 Test="{Binding Source={StaticResource typeData},XPath=/Root/Unions/Item}"
                                 />

 

4.UCUnionList的cs里面这样写:

代码
   
     
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Localizability(LocalizationCategory.NeverLocalize)]
public object Test
{
get { return GetValue(TestProperty);
}
set
{ SetValue(TestProperty, value); }
}

public static readonly DependencyProperty TestProperty = DependencyProperty.Register("Test",
typeof(object), typeof(UCUnionList));

 

 


5.在cs代码里面检测拿到的值

var a = ucUnionList.Test;
var b = ucUnionList.DataContext;

发现这个情况
a.GetType()
{Name = "XmlElement" FullName = "System.Xml.XmlElement"}
只拿到了一个子节点,即  <Item FURN_GRP_ID="64" FURN_GRP_NAME="ddd" />

b.GetType()
{Name = "XmlDataCollection" FullName = "MS.Internal.Data.XmlDataCollection"}
拿到了所有的子节点
 <Item FURN_GRP_ID="64" FURN_GRP_NAME="ddd" />
<Item FURN_GRP_ID="61" FURN_GRP_NAME="沿海地" />
<Item FURN_GRP_ID="62" FURN_GRP_NAME="地中海" />
<Item FURN_GRP_ID="63" FURN_GRP_NAME="沙滩" />

 

求教各位,这是为什么呢?

你可能感兴趣的:(context)