【问题标题】:WPF Listbox Image binding question (again)!WPF Listbox 图像绑定问题(再次)!
【发布时间】:2010-10-19 22:02:15
【问题描述】:

这让我很生气,我无法将图像显示为列表框项:这是我的代码:

WPF:

 // listbox called lstWidgets
<ListBox.ItemTemplate>
 <DataTemplate>
    <StackPanel>
      <TextBlock Name="txtTitle" Margin="2,5,5,2" Text="{Binding name}" />
      <Image Name="imgDisp"  Source="{Binding img}" Width="50" Height="50"/>
   .....       

C#:

Class widget / props: string name, Image img (get,set)...
ObservableCollection<cls_Widget> widgets....
Image newImage = new Image();
newImage.Source = new ImageSourceConverter().ConvertFromString("")as ImageSource;
cls_Widget wdg = new cls_Widget();  
wdg.img = newImage
wdg.name = "My Widget";
widgets.Add(wdg);                  
lstWidgets.ItemsSource = widgets;
....

文本块文本显示,但图像不显示(图像区域为空白).. 我很感激任何帮助!我已经得到和图像在不同的代码场景中显示,但不是这个......

提前致谢。

【问题讨论】:

    标签: wpf image binding listbox


    【解决方案1】:

    如果您将图像的 Source 绑定到支持属性,则支持属性应该是图像源,而不是图像。

    或者,您可以使用 ContentControl 在对象中显示图像。试试这个:

    ...
    <TextBlock Name="txtTitle" Margin="2,5,5,2" Text="{Binding name}" />
    <ContentControl Name="imgDisp" Width="50" Height="50" Padding="0"
                    Content="{Binding img}"/>
    ...
    

    【讨论】:

      【解决方案2】:

      我认为您需要公开 imageSource,而不是 Image。 您已经在模板中有一个图像。

      顺便说一下,查看 Visual Studio 中的调试输出可能表明绑定失败的原因。

      【讨论】:

      • 啊老输出窗口(显示绑定错误)..我将类道具更改为图像源,它工作.. Mucho Gracias
      猜你喜欢
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多