我將一個ListView綁定到一個對象列表,如下所示;使用XAML將System.Drawing.Image綁定到System.Windows.Image控件
<ListView
ItemsSource="{ Binding Path=. }"
ItemTemplate="{DynamicResource EventTemplate}">
我綁定到聲明兩個屬性的對象;
string DisplayName { get; }
System.Drawing.Image Image { get; set; }
我想填充DataTemplate
但我無法弄清楚如何;如果我在我的模板中這樣做;
<StackPanel Orientation="Horizontal">
<Image Source="{ Binding Path=Image }" />
<TextBlock Text="{ Binding Path=DisplayName }" />
</StackPanel>
出現文字,但圖像沒有。我究竟做錯了什麼?調試輸出顯示
System.Windows.Data Error: 1 : Cannot create default converter
to perform 'one-way' conversions between types
'System.Drawing.Image' and 'System.Windows.Media.ImageSource'.
Consider using Converter property of Binding.
BindingExpression:Path=Image; DataItem='RealElement'
(HashCode=54308798); target element is 'Image' (Name='');
target property is 'Source' (type 'ImageSource')
我有一個關於內存使用情況的問題:這個轉換是否只發生一次,或者每次圖像顯示在ListBox的可見部分? – emesx 2011-12-30 22:12:34
無法真正說出 - WPF需要每次都進行轉換 - 所以我認爲這只是一個內部實現細節。我認爲它不會有任何理由被一遍又一遍地轉換,但它可能值得堅持在一個櫃檯,並看到...... – 2012-01-06 00:30:29
'memoryStream'永遠不會關閉或處置,這是一個關注嗎?我有一些非常相似的代碼,可能來自這個問題,但它更醜陋。 – Maslow 2015-09-02 00:19:26