仅当我单击树视图中的任何根项时,我才看到从 TreeView 控件抛出以下绑定表达式-如果单击任何其他项,则在单击之前我看不到绑定表达式root Item 然后我一直看到绑定异常。
绑定表达式路径缺少前导,“Asset”和“Trade”是 Node 类上 Name 属性的值,这是一个分层数据结构,它有一个 Node 类型的子集合等......
这些项目使用 ListCollectionView 绑定到 TreeView。
在 Win7 上运行的 .Net 4.0 WPF 应用程序。
绑定到 TreeView 的类如下:
internal class Node : INotifyPropertyChanged
{
public string Id { get; private set; }
public string Name { get; private set; }
public Node Parent { get; private set; }
public IEnumerable<Node> Children { get; private set; }
}
任何想法为什么会发生这种情况?
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“节点”(HashCode=-729436854)上找不到“资产”属性。绑定表达式:路径=资产;DataItem='节点' (HashCode=-729436854); 目标元素是'TreeView'(名称='FieldTreeView');目标属性是“NoTarget”(类型“对象”)
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“节点”(HashCode=-729436854)上找不到“交易”属性。BindingExpression:路径=贸易;DataItem='节点' (HashCode=-729436854); 目标元素是'TreeView'(名称='FieldTreeView');目标属性是“NoTarget”(类型“对象”)
这是 TreeView 的 XAML,它被用作 WPF 用户控件的一部分:
<TreeView Name="FieldTreeView"
Grid.Row="1"
Margin="0,15,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
SelectedItemChanged="FieldTreeViewOnSelectedItemChanged">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Children, Mode=OneWay}">
<Grid>
<TextBlock Text="{Binding Path=Name, Mode=OneWay}" />
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding Path=FieldMenu, Mode=OneWay}"
DisplayMemberPath="Title"
Visibility="{Binding Path=HasFieldMenu, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding}" />
<Setter Property="CommandParameter"
Value="{Binding Path=DataContext.Id, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</TreeView.ItemsPanel>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
<EventSetter Event="MouseDoubleClick" Handler="FilterTreeViewOnItemMouseDoubleClick" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
用于填充 Node 类的代码如下所示:
private void InitialiseFields()
{
_rootNode.RemoveAllChildren();
foreach (var field in _fields.OrderBy(x => x.Id))
{
NodeHelper.TokenizeIntoNodes(field, field.Id.Split(Node.IdSeperator), _menuMediator, _rootNode);
}
_nodes = _rootNode.Children.Cast<Node>().ToArray();
FieldTreeView.ItemsSource = _rootNode.Children;
}