'WPF application is unable to show the image
I am new to WPF and is going thru a tutorial. https://www.youtube.com/watch?v=6OwyNiLPDNw&list=PLrW43fNmjaQVYF4zgsD0oL9Iv6u23PI6M&index=2
I follow exactly the same steps, but images are not loaded in my application. What I am doing wrong?
Note : Folder contains the said image. When I run the application, it only shows text "Some folder" but not the icon ( where as the same coding in the tutorial shows the image ).
<Window x:Class="WpfTreeView.MainWindow"
Loaded="Window_Loaded"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TreeView x:Name="FolderView">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Image Width="20" Margin ="3" Source="Images/drive.png"/>
<TextBlock VerticalAlignment="Center" Text="Some folder"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>
code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfTreeView
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region Constructor
/// <summary>
/// Default constructor
/// </summary>
public MainWindow()
{
InitializeComponent();
}
#endregion
private void Window_Loaded(object sender, RoutedEventArgs e)
{
foreach(var drive in System.IO.Directory.GetLogicalDrives())
{
var item = new TreeViewItem();
FolderView.Items.Add(item);
}
}
}
}
Solution 1:[1]
Thanks to Klo and Clemens for reverting. I tried to re-write the code, editor had a warning for declaration xmlns:local="clr-namespace:WpfTreeView" As a suggestion, it offered to remove this line. Post removal the icons appeared again. Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PMJ |
