1
我这是从这里创建一个DataTemplate: ComboBoxes sharing Observable Collection keeps breaking显示的图像锁定源文件
<UserControl.Resources>
<DataTemplate x:Key="ImageItemTemplate">
<StackPanel Orientation="Horizontal">
<Image Height="44" Source="{Binding Path}"/>
<Label Content="{Binding Name}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<ComboBox x:Name="image1" ItemTemplate="{StaticResource ImageItemTemplate}"/>
代码:
public ObservableCollection<ImageItem> images = new ObservableCollection<ImageItem>();
Generic.ImportGrfx(tabID, image1, images);
public static void ImportGrfx(string tabID, ComboBox combo, ObservableCollection<ImageItem> items)
{
items.Clear();
try
{
string root = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
var files = Directory.GetFiles(Path.Combine(root, "input\\" + tabID), "*.png");
foreach (var file in files)
{
ImageItem item = new ImageItem();
item.Path = file;
item.Name = Path.GetFileName(file).Remove(Path.GetFileName(file).Length - 4);
items.Add(item);
}
}
catch (Exception) { }
combo.ItemsSource = items;
}
public class ImageItem
{
public string Path { get; set; }
public string Name { get; set; }
}
我有约束力这些图像的问题数据模板“锁定”图像源。含义在程序运行时我无法编辑图像...我会看到一个错误,指出图像正在使用中。有没有办法来解决这个问题?
我不需要它来监控图像变化。实际上我的程序中有一个按钮用于重新加载图像列表。但是你的代码确实为我提供了我需要的东西......'''''''' File.Copy(e.FullPath,tempFileName,true);'。谢了哥们! – 2014-10-10 21:57:51
很高兴帮助你:D – Aybe 2014-10-10 21:58:37