wpf 将图像添加到列表框项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22946615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding image to ListBox item
提问by Boardy
I am currently working on a C# WPF appplication where I am trying to add an image, followed by some text in each list item.
我目前正在开发一个 C# WPF 应用程序,我试图在其中添加一个图像,然后在每个列表项中添加一些文本。
I've got the binding working for the text but the image isn't displayed.
我已经绑定了文本,但没有显示图像。
Below is my XAML:
下面是我的 XAML:
<Window x:Class="ServerAdministrator.FtpDirectoryListing"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ServerAdministrator"
Title="FTP Directory Listing" Height="391" Width="599">
<Grid>
<StatusBar Height="30" Margin="0,322,0,0" Name="statusBar1" VerticalAlignment="Top" />
<ToolBar Height="26" Name="toolBar1" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,32,0,0" Name="textBox1" VerticalAlignment="Top" Width="517" />
<ListBox x:Name="lstDirecotryListing" Height="233" HorizontalAlignment="Left" Margin="12,61,0,0" VerticalAlignment="Top" Width="553">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:DirectoryListing}">
<StackPanel>
<TextBlock Margin="3" Text="{Binding path}" />
<ContentControl Margin="3" Content="{Binding image}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Below is my DirectoryListing class
下面是我的 DirectoryListing 类
class DirectoryListing
{
public string path {get; set;}
public Image image{get; set;}
public DirectoryListing(Image imgage, String path)
{
this.image = image;
this.path = path;
}
}
Below is how I am adding items to the listbox
下面是我如何将项目添加到列表框
Image image = new Image();
BitmapImage bi = new BitmapImage(new Uri(@"C:\Users\Chris\Documents\Visual Studio 2010\Projects\ServerAdministrator\ServerAdministrator\bin\Debug\images\directory.png"));
image.Source = bi;
lstDirecotryListing.Items.Add(new DirectoryListing(image, "hello"));
The text is getting added fine but not the image.
文本添加得很好,但不是图像。
I'm not sure if it is related but I get the following in the Console Output in VS2010
我不确定它是否相关,但我在 VS2010 的控制台输出中得到以下信息
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ComboBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ComboBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
System.Windows.Data 错误:4:无法找到引用“RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''的绑定源。BindingExpression:Path=HorizontalContentAlignment; 数据项=空;目标元素是 'ComboBoxItem' (Name=''); 目标属性是“HorizontalContentAlignment”(类型“HorizontalAlignment”) System.Windows.Data 错误:4:无法找到引用“RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''的绑定源. BindingExpression:Path=VerticalContentAlignment; 数据项=空;目标元素是 'ComboBoxItem' (Name=''); 目标属性是“VerticalContentAlignment”(类型“VerticalAlignment”
Thanks for any help you can provide
感谢您的任何帮助,您可以提供
UPDATE
更新
I've got it working thanks to Clemens answer, still using the same two variables as the path isn't the path to the image, but anyway, it is displaying the image and text now.
由于 Clemens 的回答,我已经让它工作了,仍然使用相同的两个变量,因为路径不是图像的路径,但无论如何,它现在正在显示图像和文本。
The problem is it displays the text and the picture is underneath, I need to show the picture and the text side by side, how can I do this?
问题是它显示文字而图片在下面,我需要并排显示图片和文字,我该怎么做?
回答by Clemens
Reduce your view model to this:
将您的视图模型简化为:
public class DirectoryListing
{
public string Name { get; set; }
public string Path { get; set; }
}
and change your DataTemplate to this:
并将您的 DataTemplate 更改为:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="3" Text="{Binding Name}"/>
<Image Margin="3" Source="{Binding Path}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Built-in type conversion will automatically create an ImageSource from the file path string.
内置类型转换将自动从文件路径字符串创建一个 ImageSource。

