如何在特定列的WPF中的数据网格中添加图像 [英] how to add image in data grid in WPF at particular column

查看:65
本文介绍了如何在特定列的WPF中的数据网格中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了WPF工具包进行数据网格处理,其中我有五列ICON,FirstName,LastName,EmailID,Contact ..,我想在运行时在.cs文件中的ICON列中添加图像.png. .i不能仅在该列中添加图像..
我的xaml代码是..

I have used WPF toolkit for data grid in my application where i have five column ICON,FirstName,LastName,EmailID,Contact..and i want to add image .png in ICON column at run time in .cs file...i am not getting how to add image in only that column..
my xaml code is..

<pre lang="xml"><Window x:Class="WpfApplication5.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window1" Height="450" Width="737" Loaded="Window_Loaded"
        xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Window.Resources>
        <Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="LightBlue" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="#80EEEEEE" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid Height="374" Width="554">
        <dg:DataGrid  MouseRightButtonUp="dgData_MouseRightButtonUp" AutoGenerateColumns="True" Margin="27,36,39,50" x:Name="dgData" RowHeight="25">
            <dg:DataGrid.ContextMenu>

                <ContextMenu>
                    <MenuItem Header="Add" Click="Ass_Lesson1">
                    </MenuItem>
                </ContextMenu>
            </dg:DataGrid.ContextMenu>
        </dg:DataGrid>
    </Grid>
</Window


>



我的.cs代码是


>



my .cs code is

public partial class Window1 : Window
   {
       List<Employee> employeeList = new List<Employee>();
       public class Employee
       {
           public string ICON { get; set; }
           public string FirstName { get; set; }
           public string LastName { get; set; }
           public string EmailID { get; set; }
           public string Contact { get; set; }
       }
       public Window1()
       {
           InitializeComponent();


           for (int i = 1; i <= 5; i++)
           {
               Employee emp = new Employee
               {
                   ICON = @"D:\play.png",
                   FirstName = "FirstName" + i.ToString(),
                   LastName = "LastName" + i.ToString(),
                   EmailID = "FirstName " + i.ToString() + ".LastName" + i.ToString() + "@some.com",
                   Contact = "9999999" + i.ToString()
               };
               employeeList.Add(emp);
           }
           dgData.ItemsSource = employeeList;


       }

推荐答案

我不确定您可以使用自动生成的列,如果您的ICON属性是字符串值,则肯定无法使用.

尝试将ICON属性更改为Uri ICON = new URI(stringvalue,relativeorabsolute);,这将至少告诉数据网格期望的数据类型!
I''m not sure you can using autogenerate columns, it certainly won''t work if your ICON property is a string value.

Try changing the ICON property to a Uri ICON = new URI(stringvalue,relativeorabsolute); this will atleast tell the datagrid what datatype to expect!


这篇关于如何在特定列的WPF中的数据网格中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆