【问题标题】:Databind a listbox - one object member needs special processing数据绑定列表框 - 一个对象成员需要特殊处理
【发布时间】:2011-08-28 23:47:33
【问题描述】:
<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,当 ListBoxItems 首次使用数据初始化时,会命中一个可以访问 ListBoxItem 的 MyObject 和它的 MyImage 的函数。

【问题讨论】:

    标签: c# wpf data-binding listbox


    【解决方案1】:

    您听说过 ValueConverters 吗?这些允许您在绑定过程中转换属性。以下示例展示了如何将 URI 转换为 BitmapImage,您可以将其用作 Image 元素的 Source:

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

    你应该可以使用类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 2013-10-26
      • 1970-01-01
      相关资源
      最近更新 更多