【问题标题】:HierarchicalDataTemplate and TreeView binding compilation errorHierarchicalDataTemplate 和 TreeView 绑定编译错误
【发布时间】:2009-01-13 13:08:47
【问题描述】:

我已经实现了一个 UserControl 并在上面放置了一个 TreeView 控件。 XAML 文件如下所示。我正在使用 HierarchicalDataTemplate 与 Treeview 进行数据绑定,

<UserControl x:Class="Trinity.Windows.Viewer.Alarm.AlarmPage.TrinityDeviceTree"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src ="clr-namespace:Trinity.Windows.Viewer.Alarm.AlarmPage" 
    Height="300" Width="300">
    <UserControl.Resources>
        <src:SiteList x:Key="SiteListKey"/>
        <HierarchicalDataTemplate DataType="{x:Type src:Site}" 
            ItemsSource="{Binding Path=PartitionsList}">
        <TextBlock Text="{Binding Path=Name}"/>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type src:Partition}" 
        ItemsSource="{Binding Path=MasterDeviceList}">
        <TextBlock Text="{Binding Path=Name}"/>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type src:MasterDevice}" 
        ItemsSource="{Binding Path=SlaveDeviceList}">
        <TextBlock Text="{Binding Path=Name}"/>
    </HierarchicalDataTemplate>

    <DataTemplate DataType="{x:Type src:SlaveDevice}">
        <TextBlock Text="{Binding Path=Name}"/>
    </DataTemplate> 
</UserControl.Resources>

<Grid Width="auto" Height="auto">
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TreeView Name="DeviceTree"/>
</Grid>

我将数据类绑定到 TreeView,如下所示:

namespace Trinity.Windows.Viewer.Alarm.AlarmPage
{

    public class Site
    {
        public Site(string name)
        {
            _name = name;
            _partitions = new ObservableCollection<Partition>();
        }
        string _name;
        public string Name { get { return _name; } }
        ObservableCollection<Partition> _partitions;
        public List<Partition> PartitionsList { get { return _partitions; } }
    }

    public class Partition
    {
        public Partition(string name)
        {
            _name = name;
            _masterdevice = new ObservableCollection<MasterDevice>();
        }
        string _name;
        public string Name { get { return _name; } }
        ObservableCollection<MasterDevice> _masterdevice;
        public ObservableCollection<MasterDevice> MasterDeviceList { get { return _masterdevice; } }
    }

    public class MasterDevice
    {
        public MasterDevice(string name)
        {
            _name = name;
            _slavedevice = new ObservableCollection<SlaveDevice>();
        }
        string _name;
        public string Name { get { return _name; } }
        ObservableCollection<SlaveDevice> _slavedevice;
        public ObservableCollection<SlaveDevice> SlaveDeviceList { get { return _slavedevice; } }
    }

    public class SlaveDevice
    {
        public SlaveDevice(string name)
        {
            _name = name;
        }

        string _name;

        public string Name { get { return _name; } }
    }

    public class SiteList : ObservableCollection<Site>
    {
        public Site this[string name]
        {
            get
            {
                foreach (Site objSite in this)
                    if (objSite.Name == name)
                        return objSite;
                return null;
            }
        }
    }
}

但是现在当我编译这段代码时,我得到了以下错误:

错误 MC1000:未知的构建错误, '无法加载类型 'System.ComponentModel.IEditableCollectionView' 从程序集'WindowsBase, 版本=3.0.0.0,文化=中性, PublicKeyToken=31bf3856ad364e35'。线 9 位置 87。在 xml 文件中。

现在,如果我在 XAML 文件中注释包含 HierarchicalDataTemplate 的代码,我的项目编译时不会出错。我不明白为什么会出现编译错误。

请帮助我。

【问题讨论】:

    标签: wpf xaml compiler-errors


    【解决方案1】:

    我可以根据您的示例创建一个编译解决方案。

    该错误可能意味着您的项目目标与已安装框架或类似框架之间的版本不匹配。

    另一个问题可能是您的 PC 上的框架安装普遍受到影响。尝试在全新安装时重现错误。

    【讨论】:

      【解决方案2】:

      您不只需要针对 3.5 框架吗?或者确保您已安装 SP2 of 3.0。

      【讨论】:

      【解决方案3】:

      解决了...windowsbase.dll 不匹配...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-19
        • 1970-01-01
        • 2012-05-04
        • 2010-12-15
        相关资源
        最近更新 更多