我在MVVM项目中遇到了设计师的问题。
我有TreeView
个自定义DataTemplate
:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Name="img" Width="20" Height="20" Stretch="Fill"
Source="{Binding
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type TreeViewItem}},
Path=Header,
Converter={StaticResource HeaderToImageConverter}}"
/>
<TextBlock Text="{Binding}" Margin="5,0" />
</StackPanel>
</DataTemplate>
资源声明:
<Window x:Class="BlobWorld.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:Core="clr-namespace:BlobWorld;assembly="
xmlns:helper="clr-namespace:BlobWorld.Helper"
mc:Ignorable="d"
Title="MainWindow" Height="350.459" Width="746.561"
DataContext="{DynamicResource MainWindowViewModel}">
<Window.Resources>
<helper:HeaderToImageConverter x:Key="HeaderToImageConverter"/>
</Window.Resources>
我的转换器是:
public class HeaderToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value as string).Contains(@"."))
{
Uri uri = new Uri("pack://application:,,,/images/File.png");
BitmapImage source = new BitmapImage(uri);
return source;
}
else
{
if (!(value as string).Contains(@":"))
{
Uri uri = new Uri("pack://application:,,,/images/folder.png");
BitmapImage source = new BitmapImage(uri);
return source;
}
else
{
Uri uri = new Uri("pack://application:,,,/images/diskdrive.png");
BitmapImage source = new BitmapImage(uri);
return source;
}
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Cannot convert back");
}
}
它在运行时完美运行,但是当我使用xaml&#34; design&#34;在Visual Studio中的窗口而不是看到我的Windows的外观,我只有一个IOException : Cannot locate resource 'images/folder.png'
我的问题来自哪里? 我该如何解决?
答案 0 :(得分:3)
我注意到这个问题从未得到解决,我遇到了同样的问题,需要解决。解决此问题的方法如下:
更改:
pack://application:,,,/path/to/images/mypng.png
收件人:
/Project Namespace;component/path/to/images/mypng.png
就是这样!还要确保将图像上的 Build Action 设置为 Resource ,并且将 Copy to Output Directory 设置为请勿复制(由于这是资源,因此无需将映像复制到输出目录)。现在,您的控件将以设计模式显示。
答案 1 :(得分:0)
您可以检查它是否在DesignMode上运行,如下所示;
public class HeaderToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
if (!designMode)
{
if ((value as string).Contains(@"."))
{
Uri uri = new Uri("pack://application:,,,/images/File.png");
BitmapImage source = new BitmapImage(uri);
return source;
}
else
{
if (!(value as string).Contains(@":"))
{
Uri uri = new Uri("pack://application:,,,/images/folder.png");
BitmapImage source = new BitmapImage(uri);
return source;
}
else
{
Uri uri = new Uri("pack://application:,,,/images/diskdrive.png");
BitmapImage source = new BitmapImage(uri);
return source;
}
}
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Cannot convert back");
}
}