数据绑定列表框 - 一个对象成员需要特殊处理

本文关键字:处理 成员 列表 一个对象 数据绑定 | 更新日期: 2023-09-27 17:56:51

<ListBox Name="DisplayItemListBox">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <ListBoxItem  >
         <StackPanel Orientation="Horizontal">
           <TextBlock Text="{Binding Path=Response}"  />
             <Button Width="50" Height="50" Content="Remove" Click="Request_Remove_Click"/>
             <Image Name="MyImage" Width="50" Height="50"  />
             </StackPanel>
       </ListBoxItem>
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

我像这样绑定代码隐藏:

DisplayItemListBox.ItemsSource = (List<MyObject>) MyObjectList; 

MyObject 有一个二进制 Photo 属性,我需要在代码隐藏中将其转换为 BitmapImage。 我需要以这样一种方式修改我的 XAML,即当首次使用数据初始化列表框项时,将命中一个可以访问列表框项的 MyObject 及其 MyImage 的函数。

数据绑定列表框 - 一个对象成员需要特殊处理

你听说过ValueConverters吗?这些允许您在绑定过程中转换属性。下面的示例演示如何将 URI 转换为可用作 Image 元素的源的 BitmapImage:

http://www.eggheadcafe.com/sample-code/SilverlightWPFandXAML/03d69c15-172b-4098-bb90-5119f9bdac24/silverlight-ivalueconverter-for-image-urls.aspx

您应该能够使用类似的东西。