我复制了此YouTube视频中制作的项目:https://www.youtube.com/watch?v=6owynilpdnw&t = 3602S
但是,设计师显示"无效的标记",尽管该代码完美地工作并且建筑成功。(YouTube-Video中的那个人遇到了相同的问题(在59:43-59:56中),但以某种方式解决了。我仍然有同样的问题。)
XAML给出了蓝色下划线:source =" {binding,显示错误:名称" headertoimageconverter"名称中不存在命名空间" clr-namespace:wpftreeview"。程序仍然可以运行良好,我只是不don't参见设计师。
我试图通过尝试以下内容来解决这个问题:
- 调试/发行版和x64/x84平台之间的配置
- 删除.bin .obj和.vs文件夹
- 删除ShadowCache文件夹
- 多次干净,重建和构建解决方案
- 重置视觉工作室设置和重新启动计算机
xaml:
<Window x:Class="WpfTreeView.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:WpfTreeView"
Loaded="Window_Loaded"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView x:Name="FolderView">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="20" Margin="3"
Source="{Binding
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}},
Path=Tag,
Converter={x:Static local:HeaderToImageConverter.Instance}}" />
<TextBlock VerticalAlignment="Center" Text="{Binding}" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>
c#(headertoimageconverter类):
using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;
namespace WpfTreeView
{
/// <summary>
/// Converts a full path to a specific image type of a drive, folder or file
/// </summary>
[ValueConversion(typeof(string), targetType: typeof(BitmapImage))]
public class HeaderToImageConverter : IValueConverter
{
public static HeaderToImageConverter Instance = new HeaderToImageConverter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Get the full path
var path = (string)value;
// If the path is null, ignore
if (path == null)
return null;
// Get the name of the file/folder
var name = MainWindow.GetFileFolderName(path);
// By default, we presume an image
var image = "Images/Checkmark.png";
// If the name is blank, we presume it's a drive as we cannot have a blank file or folder name
if (string.IsNullOrEmpty(name))
image = "Images/Arrow.png";
else if (new FileInfo(path).Attributes.HasFlag(FileAttributes.Directory))
image = "Images/Checkmark.png";
return new BitmapImage(new Uri($"pack://application:,,,/{image}"));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
就个人而言,我发现Converter={x:Static local:HeaderToImageConverter.Instance}}" />
解决转换器的奇怪方法。您也可以在app.xaml中添加转换器,例如
<Application x:Class="WpfTreeView.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpftreeview="clr-namespace:WpfTreeView"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<wpftreeview:HeaderToImageConverter
x:Key="HeaderToImageConverter "/>
</ResourceDictionary>
</Application.Resources>
接下来,用
将转换器引用Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=Tag, Converter={StaticResource HeaderToImageConverter}}" />
编辑:
您可以尝试将汇编添加到命名空间声明中吗? xmlns:local="clr-namespace:WpfTreeView;assembly=MyAssembly