对列表框进行数据绑定 - 一个对象成员需要特殊处理
<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 有一个二进制照片属性,我需要在代码隐藏中将其转换为 BitmapImage。我需要以这样的方式修改我的 XAML,当 ListBoxItems 首次使用数据初始化时,会调用一个可以访问 ListBoxItem 的 MyObject 及其 MyImage 的函数。
<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>
I bind in codebehind like this:
DisplayItemListBox.ItemsSource = (List<MyObject>) MyObjectList;
MyObject has a binary Photo attribute that I need to convert to a BitmapImage in code behind. I need to modify my XAML in such a way that, when the ListBoxItems are first initialized with data, a function is hit that has access to both the ListBoxItem's MyObject and it's MyImage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您听说过价值转换器吗?这些允许您在绑定过程中转换属性。以下示例显示如何将 URI 转换为 BitmapImage,您可以将其用作图像元素的源:
http://www.eggheadcafe.com/sample-code/SilverlightWPFandXAML/03d69c15-172b-4098-bb90-5119f9bdac24/silverlight -ivalueconverter-for-image-urls.aspx
您应该能够使用类似的东西。
Have you heard of ValueConverters? These allow you to convert a property within the binding process. The following example show how to convert a URI into a BitmapImage which you can use as a Source for an Image element:
http://www.eggheadcafe.com/sample-code/SilverlightWPFandXAML/03d69c15-172b-4098-bb90-5119f9bdac24/silverlight-ivalueconverter-for-image-urls.aspx
You should be able to use something similar.