显示列表框中的图像 [英] Displaying an image from a list box
问题描述
我有一个下拉列表,其中包含不同的内容.例如:
雨伞
盖帽
衬衫
足球等.
上面可以是列表框的内容.我想做的是,当我单击雨伞时,我想在图像框中显示雨伞的图像.而且我想像列表框项==雨伞那样进行操作,然后应显示雨伞的图像.
但是我做不到!有人可以帮我提供代码吗?
感谢大家的努力:-)
I have a drop down list of different things. For example:
umbrella
cap
shirt
football etc..
The above can be the contents of the list box. What I want to do is that when I click on umbrella, I want to display the image of umbrella in the image box. And I want to do it in the way as if the list box item == umbrella, then the image of umbrella should display.
But I can''t do this! Can some body please help me with the code?
Everybody''s effort is appreciated :-)
推荐答案
以下是一些示例WPF代码:
Here''s some sample WPF code:
<StackPanel Orientation="Vertical">
<ListBox ItemsSource="{Binding Objects}"
SelectedIndex="{Binding CurrentIndex}" />
<Image Source="{Binding ObjectImage}" />
</StackPanel>
这是视图模型(或数据源)中的相应代码:
Here''s the corresponding code in the view-model (or data source) :
public string[] Objects { get; set; }
private int currentIndex;
public int CurrentIndex
{
get { return currentIndex; }
set
{
if (currentIndex == value)
return;
currentIndex = value;
FirePropertyChanged("CurrentIndex");
UpdateObjectImage();
}
}
private ImageSource objectImage;
public ImageSource ObjectImage
{
get { return objectImage; }
set
{
if (objectImage == value)
return;
objectImage = value;
FirePropertyChanged("ObjectImage");
}
}
private void UpdateObjectImage()
{
if (CurrentIndex >= Objects.Length)
return;
this.ObjectImage = GetImage(Objects[CurrentIndex]);
}
private ImageSource GetImage(string name)
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(
String.Format(
"pack://application:,,,/YourWpfApplicationName;component/{0}.png",
name));
image.EndInit();
return image;
}
通过对我以前对Hanifuk的答复的评论作出澄清后,答案非常不同:
首先,您需要为项目创建一些类以存储在列表中.您的需求意味着它具有双重目的:在列表中显示一些文本,并提供图像显示信息.假设这是图像文件名(可能更复杂;这仅是示例).要在列表中显示该项目,您需要它来覆盖object.ToString()
:
Very different answer after clarification by the comment on my previous Answer by Hanifuk:
First, you need to create some class for an item to store in the list. You requirements mean that it serve dual purpose: to show some text in the list and also to provide information for image presentation. Let''s assume this is image file name (could be more complex; this is just for example). To show this item in the list, all you need it to overrideobject.ToString()
:
internal class ListItemHelper {
internal ListItemHelper(string itemText, string imageFileName) {
this.ItemText = itemText;
this.FImageFileName = imageFileName;
} //ListItemHelper
public override string ToString() {
return ItemText;
} //object.ToString()
internal string ImageFileName { get {return FImageFileName;} }
private string ItemText, FImageFileName;
} //class ListItemHelper
使用此类将信息添加到您的列表中:
Use this class to add information to your list:
MyList.Add(new ListItemHelper("First day of vacation", "2010-06-01-1343.jpg"));
//item will show int the list: First day of vacation
现在,您需要设置一个处理程序以选择列表中的项目:
Now you need to setup a handler for selection of the item in your list:
void ShowMyPicture(string source) {
/*...*/
//should be able to handle null as "nothing selected"
} //ShowMyPicture
//...
MyList.SelectionChanged += (sender, eventArgs) => {
//you know the sender is your list box, so not afraid of the cast:
ListBox listBox = (ListBox)sender;
//you know the item is alway ListItemHelper, so not afraid of the cast:
if (listBox.SelectedItem == null)
ShowMyPicture(null);
else {
ListItemHelper helper = (ListItemHelper)listBox.SelectedItem;
ShowMyPicture(helper.ImageFileName); //you probaly know what to do
} //if
}; //MyList.SelectionChanged handler
就这样.
That''s it.
您几乎可以根据需要添加到列表项:
You can add to list items practically whatever you want:
MyList.Items.Add(MyImage);
这篇关于显示列表框中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!