[英]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.