询问者
图片绑定问题

问题
-
XAML绑定代码如下(使用ListBox+Stack Panel):
<ListBox Margin="10,336,-10,0" Width="Auto" Name="lstBox" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Stretch="Fill" Width="200" Height="200" Source="{Binding Source}"/>
<TextBlock Width="Auto" Text="{Binding Text}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
后台代码:这样可以运行出正常效果:
Question:当我把“MyItem”类的public改成private,并且移动到MainPage类中,却无法绑定显示了,是为什么呢?
namespace WindowsPhoneApplicationCSharp
{
/// <summary>
/// 这是无法显示的代码
/// </summary>
public partial class MainPage : PhoneApplicationPage
{
private class MyItem
{
public ImageSource Source { get; set; }
public string Text { get; set; }
}
//以下全部一样,我省略了………………
}
}
如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处2011年9月30日 7:33
全部回复
-
因为在与XAML对应的MainPage类中需要定义名称为Source和Text 的数据源。
Cedar自定义类中不可以吗?
我的自定义类只是private的就不可以,但是public就可以的。请仔细看我最初的详细描述。谢谢
如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处2011年10月1日 4:11 -
哦,这样啊。
没能为您合理的解释,很抱歉了。
Cedar
如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处2011年10月1日 6:51 -
呵呵,不是。是:
1)public 改成private。
2)移动到MainPage中
就不行了。
我用Beta版本,是不是和这个有关,要不我下载正式版看看
如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已编辑 ThankfulHeart 2011年10月1日 6:59
2011年10月1日 6:58 -
为您提供我所采用的有关图片的数据模板的代码,希望能对您有所帮助。
<DataTemplate x:Key="ImageTemplate">
<StackPanel Margin="0,3,20,12">
<Border BorderBrush="Red" BorderThickness="2" Margin="0">
<Image
Source="{Binding BitMapSource}"
Stretch="None"
Height="173" Width="173"
/>
</Border>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left" Margin="0,0,0,3" Foreground="White" />
</StackPanel>
</DataTemplate>在Panorama Item中的代码
<controls:PanoramaItem Header="pictures" Name="CustomsItem">
<ListBox
Name="LandscapeListbox"
ItemsSource="{Binding Images, Mode=TwoWay}"
ItemTemplate="{StaticResource ImageTemplate}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</controls:PanoramaItem>在MainPage.xaml.cs中定义绑定ListBox的Images,以及ImageTemplate的BitMapSource和Name。
Cedar- 已编辑 XuesongGao 2011年10月1日 7:04
2011年10月1日 7:00 -
Images的定义方法
public ObservableCollection<ImageItem> Images
{
get { return (ObservableCollection<ImageItem>)GetValue(ImagesProperty); }
set { SetValue(ImagesProperty, value); }
}BitMapSource和Name定义在ImageItem类中
public class ImageItem
{
public ImageItem(string fileName)
{
BitmapImage imageTemp = new BitmapImage();
Uri picUri = new Uri(fileName, UriKind.Relative);imageTemp.UriSource = picUri;
BitMapSource = imageTemp;
if (!string.IsNullOrEmpty(fileName))
{
Name = System.IO.Path.GetFileNameWithoutExtension(fileName);
}
}public BitmapImage BitMapSource { get; set; }
public string Name { get; set; }
}重新审视我的代码,看来没能解释为什么public变为private,移动至MainPage类就无法成功实现数据绑定。而是也采用的是public方法,以及单独的类定义Image的DataTemplate。
Cedar- 已标记为答案 世外涛缘MVP 2011年11月30日 8:34
- 取消答案标记 ThankfulHeart 2011年11月30日 8:35
2011年10月1日 7:13