来自资源的图像源 [英] Image Source from Resources
问题描述
我需要一个4列的DataGrid。其中之一应显示图像。
为此,我使用了DataGridTemplateColumn,其他的都是简单的TextColumns。
我每行需要不同的图像,这就是为什么我需要将它们与我班级中其他财产绑定在一起的原因。
I need a DataGrid with 4 columns. One of them should show an Image. I used DataGridTemplateColumn for this and the others are simple TextColumns. I need different images per row and this is why I need to bind them with the rest of my property's in my Class.
我尝试使用设置时图像类型的属性
I've tried using a property of type Image while setting
AutoGenerateColumns = true
AutoGenerateColumns="true"
,并且我已经厌倦了用一个填充了项目资源路径的字符串属性来绑定图像路径,但这也不起作用。
and I've tired binding the images path with a string property filled with the path from my project resources but this didn't work either.
有谁知道如何解决这个问题?
Does anyone know how to solve this ?
public List<MyClass> MyCollection {get; set;}//<--- This is what I bind !
public class MyClass
{
public string A { get; set; }
public string ImagePath { get; set; }
public int X { get; set; }
public string User { get; set; }
}
我的XAML是这样的:
My XAML is this:
<Window.Resources>
<CollectionViewSource x:Key="EntryCollection" Source="{Binding Path=MyCollection , Mode=OneWay}"/>
</Window.Resources>
<DataGrid ItemsSource="{Binding Source={StaticResource EntryCollection}, Mode=OneWay}" SelectedItem="{Binding Path=SelectedEntry, Mode=TwoWay}"
IsReadOnly="true"
AutoGenerateColumns="False"
SelectionUnit="FullRow"
SelectionMode="Extended"
HorizontalAlignment="Stretch"
Grid.Column="0"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
Width="Auto">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Image" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Width="16" Height="16" Source="{Binding Path=ImagePath, Mode=OneWay}" VerticalAlignment="Top" HorizontalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="User" Binding="{Binding Path=User, Mode=OneWay}" Width="Auto" />
<DataGridTextColumn Header="StringA" Binding="{Binding Path=A, Mode=OneWay}" Width="Auto" />
<DataGridTextColumn Header="INT X" Binding="{Binding Path=X, Mode=OneWay}" Width="Auto" />
预先感谢!
推荐答案
您的ImagePath属性应为
Your ImagePath property should be
ImagePath="pack://application:,,,/<your resources>/yourImage.png";
这篇关于来自资源的图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!