wpf 在 DataGrid-Column 中显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23952174/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:48:36  来源:igfitidea点击:

Display an image in a DataGrid-Column

c#wpfmvvmbindingdatagrid

提问by xeraphim

I'm trying to display an image in a DataGrid-Column next to other data. My model looks like this:

我正在尝试在其他数据旁边的 DataGrid-Column 中显示图像。我的模型看起来像这样:

public class Person 
{
    public string Name { get; set; }
    public string Address { get; set; }
    public Bitmap Image { get; set; }
}

The ViewModel:

视图模型:

public ObservableCollection<Person> Persons
    {
        get
        {
           return this.persons;
        }

        set
        {
            this.persons = value;
        }
    }

And my DataGrid is this:

我的 DataGrid 是这样的:

<DataGrid Name="Persons"
              Grid.Row="1"
              Grid.Column="0"
              Margin="10"
              AutoGenerateColumns="False"
              IsReadOnly="True"
              ItemsSource="{Binding Path=Persons}">
        <DataGrid.Columns>
            <DataGridTemplateColumn Width="80">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="{Binding}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Width="120"
                                Binding="{Binding Path=Name}"
                                Header="Name" />
            <DataGridTextColumn Width="120"
                                Binding="{Binding Path=Address}"
                                Header="Address" />
        </DataGrid.Columns>
    </DataGrid>

The Name and Adress get displayed correctly, but the image is empty... Do you know what I'm doing wrong?

名称和地址显示正确,但图像为空...你知道我做错了什么吗?

Thanks in advance and have a nice day

提前致谢,祝您有美好的一天

回答by dkozl

If your Imageis of a System.Drawing.Bitmapyou should change it to System.Windows.Media.Imaging.BitmapImageand then change also Image.Sourcebinding which at the moment binds to whole Personobject to its Imageproperty

如果你Image是 aSystem.Drawing.Bitmap你应该改变它System.Windows.Media.Imaging.BitmapImage,然后改变Image.Source绑定,目前绑定到整个Person对象到它的Image属性

<Image Source="{Binding Image}" />

回答by Moez Rebai

Try to use that code to display your image :

尝试使用该代码来显示您的图像:

 <DataGridTemplateColumn Width="80">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Image Source="{Binding Image}" />
            </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>

回答by Harjeet Singh

You need to set some Height and Width for the image like

您需要为图像设置一些高度和宽度,例如

<Image Source="{Binding}" Height="200" Width="200"/>

回答by Fred

xmlns:converter="clr-namespace:ProjectName.Converters"
<Window.Resource>
 <converter:BindableConverter x:Key="bindableConverter"/>
</Window.Resource>    
<DataGridTemplateColumn Header="HeaderName">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Image x:Name="BindImg" Height="35" Width="35" Source="{Binding IsBindable,Converter={StaticResource bindableConverter}}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>                             
                            </DataGridTemplateColumn> 


 public class BindableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string imageSource = string.Empty;
            bool isBinded;
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
            {
                imageSource = "../../Resource/Images/unbinded.jpg";
            }
            if (Boolean.TryParse(value.ToString(), out isBinded))
            {
                if (isBinded)
                {
                    imageSource = "../../Resource/Images/binded.jpg";
                }
                else
                {
                    imageSource = "../../Resource/Images/unbinded.jpg";
                }
            }
            return imageSource;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

回答by Vikas

Add image floder in your project and add your images in the same folder. use following code to access

在您的项目中添加 image floder 并将您的图像添加到同一文件夹中。使用以下代码访问

<Columns> 
<ItemTemplate >
<asp:ImageButton ID="ImageButton1" runat ="server" CommandName="Preview" ImageUrl="~/images/MSWord_Icon.jpg"/>
</ItemTemplate>
</Columns>