1

I have a WPF desktop application. I want to implement a file manager using TreeView structure. When I try to add a new item, I am getting not handled exception : "System.Windows.Markup.XamlParseException". How can i solve this problem? This is code 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>

Instead, the code below shows MainWindow code:

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);


        }
    }
}

}

Federico Rizzo
  • 183
  • 2
  • 9

1 Answers1

0

Use {Binding .} In that case Path will be equal to current DataContext, check the discussion under this question

ASpirin
  • 3,601
  • 1
  • 23
  • 32