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