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

问题描述

我想通过编码在数据网格视图的第一列中添加图像.我的代码是
window5.xaml

i want to add image in first column of data grid view by coding..my code is
window5.xaml

<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" Background="LightGray" RowBackground="LightGray" FontFamily="calibri">
            <dg:DataGrid.ContextMenu>

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



window5.xaml.cs



window5.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Data;

namespace WpfApplication5
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        List<Employee> employeeList = new List<Employee>();
        public class Employee
        {
            public int ID { 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
                {
                    ID = i,
                    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;
  }
        private void dgData_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            ContextMenu cxMenu = new ContextMenu();
        }
       
       private void Ass_Lesson1(object sender, RoutedEventArgs e)
        {
            Employee emp = null;
            foreach (Employee item in dgData.SelectedItems)
            {
                emp = new Employee
                {
                    ID = item.ID,
                    FirstName = item.FirstName,
                    LastName = item.LastName,
                    EmailID = item.EmailID,
                    Contact = item.Contact
                };
            }
            MessageBox.Show(emp.ID.ToString());
        }
    }
}

推荐答案

<dg:DataGrid  MouseRightButtonUp="dgData_MouseRightButtonUp" AutoGenerateColumns="True" Margin="27,36,39,50" x:Name="dgData" RowHeight="25" Background="LightGray" RowBackground="LightGray" FontFamily="calibri">
            <dg:DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Add" Click="Ass_Lesson1">
                    </MenuItem>
                </ContextMenu>
            </dg:DataGrid.ContextMenu>
<dg:datagrid.columns xmlns:dg="#unknown">
 <dg:datagridtemplatecolumn header="First Column">
    <dg:datagridtemplatecolumn.celltemplate>
       <datatemplate>
          <Image Source="{Binding Path=MyImage}" />
       </datatemplate>
    </dg:datagridtemplatecolumn.celltemplate>
 </dg:datagridtemplatecolumn>
</dg:datagrid.columns>
          </dg:DataGrid>


其中MyImage是保存您Image(BitmapImage)的属性.

希望对您有所帮助.


Where MyImage is the property which holds you Image(BitmapImage).

Hope it helped.


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

09-17 19:03