我看到以下绑定表达式仅在我单击树视图中的任何根项目时从TreeView控件中抛出 - 如果我单击任何其他项目我看不到绑定表达式,直到我单击根项,然后我一直看到绑定异常。
缺少绑定表达式路径,'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路径错误:'资产' 'object'''Node'(HashCode = -729436854)'找不到属性。 BindingExpression:路径=资产; DataItem ='Node'(HashCode = -729436854); target元素是'TreeView'(Name ='FieldTreeView');目标财产 是'NoTarget'(类型'对象')
System.Windows.Data错误:40: BindingExpression路径错误:'object'上找不到'Trade'属性 ''Node'(HashCode = -729436854)'。 BindingExpression:路径=贸易; DataItem ='Node'(HashCode = -729436854);目标元素是'TreeView' (名称= 'FieldTreeView'); target属性是'NoTarget'(类型'Object')
这是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;
}