![](/img/trans.png)
[英]How do I fix "Two-way binding requires Path or XPath" exception in WPF Datagrids?
[英]How do i fix exception "Two-way binding requires Path or XPath
我有一个WPF桌面应用程序。 我想使用TreeView结构实现文件管理器。 当我尝试添加新项目时,出现未处理的异常:“ System.Windows.Markup.XamlParseException”。 我怎么解决这个问题? 这是代码XAML:
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Loaded="Window_Loaded"
Title="MainWindow" Height="600" Width="400">
<Grid Margin="5,0,-5,0">
<TreeView x:Name="Folder_View">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="50" Margin="5" Source="C:\Users\Federico\Documents\Visual Studio 2017\Projects\WpfApp2\WpfApp2\Images\folder.png"/>
<TextBox VerticalAlignment="Center" Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
</Grid>
而是,下面的代码显示MainWindow代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var localDrives = Directory.GetLogicalDrives();
System.Console.WriteLine(localDrives);
foreach (var drive in Directory.GetLogicalDrives())
{
var item = new TreeViewItem()
{
Header = drive,
Tag = drive
};
Folder_View.Items.Add(item);
}
}
}
}
使用{Binding .}
在这种情况下,Path将等于当前的DataContext,请检查此问题下的讨论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.