【问题标题】:MVVM bind XML document to TreeViewMVVM 将 XML 文档绑定到 TreeView
【发布时间】:2015-05-13 14:26:22
【问题描述】:

在阅读了很多非常相似的帖子之后,我无法让它工作。我的问题似乎发生在绑定中,但我看不出有什么问题。我正在使用 MVVM 模式,并尝试将 XML 文件显示到 UserControl 的 TreeView 中。

这是我的 View XAML 代码

<UserControl x:Class="OtdrQualifTools.View.EepromView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              
             xmlns:app="clr-namespace:OtdrQualifTools"
             xmlns:v="clr-namespace:OtdrQualifTools.View"             
             mc:Ignorable="d" 
             HorizontalAlignment="Stretch"
             VerticalAlignment="Stretch"
             Background="#FF2D2D30" d:DesignHeight="600" d:DesignWidth="1030">
    <UserControl.Resources>
        <HierarchicalDataTemplate x:Key="NodeTemplate">
            <TextBlock x:Name="text" Text="?" />
            <HierarchicalDataTemplate.ItemsSource>
                <Binding XPath="child::node()" />
            </HierarchicalDataTemplate.ItemsSource>
            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                    <Setter TargetName="text" Property="Text" Value="{Binding Path=Value}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                    <Setter TargetName="text" Property="Text" Value="{Binding Path=Name}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Document">
                    <Setter TargetName="text" Property="Text" Value="{Binding Path=DocumentName}"></Setter>
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
        </HierarchicalDataTemplate>

        <!-- Test to load directly the XML file here in the resources -->
        <XmlDataProvider x:Key="xmlData" XPath="*" Source="E:/Projects/Irma/blah blah blah/xmlFile.xml" />
    </UserControl.Resources>
    <Grid>
        <Button Command="{Binding StartCommand}" Content="Go" Margin="5" Width="100" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <TreeView Margin="10,50,10,10"
                  ItemsSource="{Binding XmlEeprom}"
                  ItemTemplate= "{StaticResource NodeTemplate}"/>
        <!-- Use this part works!
        <TreeView Margin="10,50,10,10"
                  ItemsSource="{Binding Source={StaticResource xmlData}, XPath=*}"
                  ItemTemplate= "{StaticResource NodeTemplate}"/>
        -->
    </Grid>
</UserControl>

还有 ViewModel 类

   public class EepromViewModel : WorkspaceViewModel
   {
      private System.Windows.Data.XmlDataProvider _xmlEeprom;
      public System.Windows.Data.XmlDataProvider XmlEeprom
      {
         get { return this._xmlEeprom; }
         set { this._xmlEeprom = value; OnPropertyChanged("XmlEeprom"); }
      }
      protected override void Start()
      {
         System.Windows.Data.XmlDataProvider dataProvider = new System.Windows.Data.XmlDataProvider();

         dataProvider.Source = new Uri(@"E:\Projects\Irma\blah blah blah\xmlFile.xml");
         dataProvider.IsInitialLoadEnabled = true;
         dataProvider.IsAsynchronous = false;
         dataProvider.XPath = "*";

//         //Test with XmlDocument directly faild too 
//         XmlDocument xmlDoc = new XmlDocument();
//         xmlDoc .LoadXml(
//               @"<root>
//                    <child1>text1</child1>
//                    <child2>text2</child2>
//                  </root>");
//         dataProvider.Document = xmlDoc ;
         this.XmlEeprom = dataProvider;
      }
   }

当我跟踪代码时,DataProvider 加载良好,文档存在。为什么在 treeView 中不显示?

【问题讨论】:

  • 不知道你用的是哪个组件。是telerik吗?您的模型属性是否可以通知(实现 INotifyPropertyChanged)?检查这个。
  • 更重要的是你的Enumerable Items 必须支持 ICollectionChange ,它可以是 ObservableCollection 之类的东西
  • 我的 ViewModel 实现了 INotifyPropertyChanged,它确实有效。我正在尝试将 ViewModel 中的 XmlDataProvider 绑定到 TreeView 的 ItemsSource,但它不起作用...
  • 输出窗口中是否有任何绑定错误?
  • 遗憾的是没有...我验证的第一个地方

标签: c# wpf xaml mvvm treeview


【解决方案1】:

我找到了解决方案(碰巧!)。

我只是将绑定从

<TreeView Margin="10,50,10,10"
                  ItemsSource="{Binding XmlEeprom}"
                  ItemTemplate= "{StaticResource NodeTemplate}"/>

<TreeView Margin="10,50,10,10"
                  ItemsSource="{Binding XmlEeprom.Document}"
                  ItemTemplate= "{StaticResource NodeTemplate}"/>

现在它就像一个魅力!

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 2017-01-02
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多