如何在类中定义的事件处理程序中使用属性,该属性在Observable Collection中使用。
我的代码是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
namespace BindingImages
{
class MyImage
{
private string _path;
public MyImage(string path)
{
_path=path;
}
public string Path
{
get { return _path;}
}
}
class MyPhotos:ObservableCollection<MyImage>
{
DirectoryInfo _directory;
public string Path
{
set
{
_directory = new DirectoryInfo(value);
Update();
}
get { return _directory.FullName; }
}
private void Update()
{
foreach (FileInfo f in _directory.GetFiles("*.jpg"))
{
Add(new MyImage(f.FullName));
}
}
}
}
这是我的窗口代码:
namespace BindingImages
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
this.DataContext = new MyPhotos();
InitializeComponent();
}
private void listbox1_SelectionChanged(object sender,SelectionChangedEventArgs args)
{
string selection=listbox1.SelectedItem.ToString();
if ((selection != null) && (selection.Length != 0))
{
ImageSourceConverter imgConv = new ImageSourceConverter();
ImageSource imagesource=(ImageSource)imgConv.ConvertFromString(selection);
img.Source = imagesource; // here i'm having a problem
}
}
}
}
这是我的Xaml代码:
<Window x:Class="BindingImages.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ph="clr-namespace:BindingImages"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ph:MyPhotos x:Key="Photo" Path="D:\Photos" />
</Window.Resources>
<Grid>
<TextBlock FontSize="26" HorizontalAlignment="Center">Pick an Image From below List</TextBlock>
<ListBox Height="250" Width="420" Margin="10,0,10,480" SelectionChanged="listbox1_SelectionChanged" Name="listbox1" ItemsSource="{Binding Source={StaticResource Photo}}">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Path}" Width="100" Height="100"></Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Image Height="250" Margin="155,50,12,14" Name="img" Stretch="Fill" ></Image>
</Grid>
</Window>
所以当我从列表框中选择任何图像时,我必须触发一个事件,以便列表框中的选定图像显示为单个图像?
我在处理事件时遇到问题,因为你可以看到处理事件的代码。??