【发布时间】:2011-09-24 10:04:26
【问题描述】:
我试图在标签中显示照片对象的名称,但我在特定控件 System.Windows.Image 中显示此照片,此控件具有与照片集合的 CurrentItem 的绑定,所以如果我有一个按钮来显示下一张照片,我想在标签中显示照片的名称(集合的 currentItem !!!)或该元素内的另一个属性。
公开课照片 { 姓名姓名; BitmapFrame _image;
public Photo(string path)
{
Name = path;
_image = BitmapFrame.Create(_source);
}
public BitmapFrame ImageFrame { get { return _image; } set { _image = value; } }
}
公共部分类 ImageViewer:窗口 { int currentIndex = 0; ObservableCollection 照片;
public ImageViewer()
{
InitializeComponent();
photos = new ObservableCollection<Photo>();
Photo photo1 = new Photo(@"C:/Users/.../DSC00700.jpg");
Photo photo2 = new Photo(@"C:/Users/../DSC00581.jpg");
Photo photo3 = new Photo(@"C:/Users/../3882_6.jpg");
photos.Add(photo1);
photos.Add(photo2);
photos.Add(photo3);
this.DataContext = photos;
}
private void NextPhoto(object sender, RoutedEventArgs e)
{
currentIndex++;
if (currentIndex == photos.Count)
currentIndex = 0;
image.Source = photos[currentIndex].ImageFrame;
}
}
xaml 代码:
<Image
Grid.Column="0"
Grid.Row="0"
Name="image"
Stretch="Fill"
DataContext ="{Binding /}"
Source="{Binding ImageFrame}"
>
</Image>
<TextBlock
DataContext="{Binding ElementName=image, Path=DataContext}"
Text="{Binding Name}">
</TextBlock>
问候 伊万
【问题讨论】:
标签: wpf wpf-controls binding