【问题标题】:Select Individual XML Bound Items in TreeView在 TreeView 中选择单个 XML 绑定项
【发布时间】:2009-10-09 18:17:15
【问题描述】:

我正在尝试学习如何将以下简单的 XML 文件绑定到 WPF TreeView

<?xml version="1.0" encoding="utf-8" ?>
<Profiles>

  <Customer>
    <Name>Customer1</Name>    
    <Profile>
      <Version>1.0</Version>
      <DisplayText>DisplayText1</DisplayText>
    </Profile>
    <Profile>
      <Version>1.0</Version>
      <DisplayText>DisplayText2</DisplayText>
    </Profile>
  </Customer>

  <Customer>
    <Name>Customer2</Name>    
    <Profile>
      <Version>1.0</Version>
      <DisplayText>DisplayText3</DisplayText>
    </Profile>    
  </Customer>

</Profiles>

这是我尝试的 XAML 代码:

 <TreeView DockPanel.Dock="Left" Height="auto" Name="treeView1" Width="217"
           SelectedItemChanged="UIProfileTreeViewSelectedItemChanged"
           ItemsSource="{Binding}">
     <TreeView.DataContext>
         <XmlDataProvider Source="Profiles.xml" XPath="/Profiles/Customer"/>
     </TreeView.DataContext>
     <TreeView.Resources>
         <DataTemplate DataType="Customer">
             <TreeViewItem Header="{Binding XPath=Name}"
                           ItemsSource="{Binding XPath=Profile}"/>
         </DataTemplate>                
         <DataTemplate DataType="Profile">
             <TreeViewItem Header="{Binding XPath=DisplayText}" />
         </DataTemplate>                
     </TreeView.Resources> 
 </TreeView>

但是,结果阻止我在TreeView 中选择客户下的个人资料,如果我单击一个资料,整个组就会像这样突出显示:

alt text http://img38.imageshack.us/img38/4484/sberr.png

我显然对数据绑定项的工作方式存在概念性错误。有什么指点吗?

【问题讨论】:

    标签: wpf visual-studio-2008 xaml data-binding treeview


    【解决方案1】:

    我能够让它与下面的 xaml 一起工作。

    我在使用您的代码时遇到了两个基本问题:

    1. 您的树视图的两个级别都使用“DataTemplate”而不是“外部”一个被定义为“HierarchicalDataTemplate”——因此您必须将 ItemsSource 放在 TreeViewItem 上,这似乎导致了“选择为组”问题
    2. 我在使用 TreeViewItems 使选择工作时遇到了麻烦,但是在将 DataTemplates 转换为显示标签后(您可以在此处显示任何类型的简单或复杂的控件集),它工作得很好。 WPF 会自动插入“TreeViewItem”控件以将您绑定的任何内容包装到 TreeView 中,因此您无需显式执行此操作。

          <TreeView DockPanel.Dock="Left" Height="auto" Name="treeView1" Width="217" SelectedItemChanged="UIProfileTreeViewSelectedItemChanged" ItemsSource="{Binding}">
            <TreeView.DataContext>
              <XmlDataProvider Source="Profiles.xml" XPath="/Profiles/Customer"/>
            </TreeView.DataContext>
            <TreeView.Resources>
              <HierarchicalDataTemplate DataType="Customer" ItemsSource="{Binding XPath=Profile}">
                  <Label Content="{Binding XPath=Name}" />
              </HierarchicalDataTemplate>
              <DataTemplate DataType="Profile">
                  <Label Content="{Binding XPath=DisplayText}" />
              </DataTemplate>
            </TreeView.Resources>
          </TreeView>
      

    希望这会有所帮助!

    【讨论】:

    • 谢谢你,GS。看起来 HierarchicalDataTemplate 是我缺少的主要内容!干杯
    猜你喜欢
    • 2013-05-25
    • 2011-02-28
    • 1970-01-01
    • 2011-07-06
    • 2017-07-09
    • 2021-10-02
    • 2010-11-26
    • 2011-11-26
    • 1970-01-01
    相关资源
    最近更新 更多