首页 > 其他分享 >WPF datagrid datagridtemplatecolumn DataGridTemplateColumn.CellEditingTemplate DataGridComboBoxColum

WPF datagrid datagridtemplatecolumn DataGridTemplateColumn.CellEditingTemplate DataGridComboBoxColum

时间:2024-05-03 21:12:36浏览次数:22  
标签:set datagridtemplatecolumn DataGridTemplateColumn get System CellEditingTemplate

//xaml
<Window x:Class="WpfApp89.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApp89"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ObjectDataProvider x:Key="workDayEnum" MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type Type="local:WeekDay"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid >
        <DataGrid ItemsSource="{Binding}"  AutoGenerateColumns="False" SelectionMode="Extended"
                  SelectionUnit="Cell" CanUserResizeColumns="True" CanUserResizeRows="True"
                  CanUserSortColumns="True"  RowDetailsVisibilityMode="Visible" FrozenColumnCount="2">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
                <DataGridTextColumn Header="Summary" Binding="{Binding Summary}"/>
                <DataGridTextColumn Header="Title" Binding="{Binding Title}"/>
                <DataGridTextColumn Header="Topic" Binding="{Binding Topic}"/> 
                <DataGridCheckBoxColumn Header="IsSold" Binding="{Binding IsSold}"/>
                <DataGridTemplateColumn Header="ISBN">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <Border BorderBrush="Blue" BorderThickness="5">
                                    <TextBlock Text="{Binding ISBN}"/>
                                </Border>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding ISBN}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Email">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock>
                                <Hyperlink NavigateUri="{Binding Email}"
                                           Click="Hyperlink_Click">
                                    <Run Text="{Binding Email,Mode=OneWay}"/>
                                </Hyperlink>
                            </TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Email}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
                <DataGridComboBoxColumn Header="WorkDay"
                                        SelectedItemBinding="{Binding WorkDay}"
                                        ItemsSource="{Binding Source={StaticResource workDayEnum}}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>


//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace WpfApp89
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InitDG();
        }

        private void InitDG()
        {
            var bookList = new List<Book>();
            for (int i = 0; i < 10000; i++)
            {
                Book bk = new Book()
                {
                    Id = i + 1,
                    ISBN = $"ISBN_{i+1}",
                    Name = $"NAME_{i+1}",
                    Summary = $"SUMMARY_{i+1}",
                    Title = $"TITLE_{i+1}",
                    Topic =$"TOPIC_{i+1}",
                    IsSold = i % 2 == 0,
                    WorkDay = GetWeekDayViaValue(i%7),
                    Email=$"Email_{i+1}@ms.com"
                };
                bookList.Add(bk);
            }
            DataContext = bookList;
        }

        public static WeekDay GetWeekDayViaValue(int value)
        {
            WeekDay tempDay;
            switch (value)
            {
                case 1:
                    tempDay = WeekDay.Monday;
                    break;
                case 2:
                    tempDay = WeekDay.Tuesday;
                    break;
                case 3:
                    tempDay = WeekDay.Wednesday;
                    break;
                case 4:
                    tempDay = WeekDay.Thursday;
                    break;
                case 5:
                    tempDay = WeekDay.Friday;
                    break;
                case 6:
                    tempDay = WeekDay.Saturday;
                    break;
                default:
                    tempDay = WeekDay.Sunday;
                    break;
            }
            return tempDay;
        }

        private void DataGrid_AutoGeneratedColumns(object sender, EventArgs e)
        {
            var gd = sender as DataGrid;
            if (gd != null)
            {
                gd.Columns[0].DisplayIndex = gd.Columns.Count - 1;
            }
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            var ur=sender as Hyperlink;
            if(ur!=null && ur.NavigateUri!=null)
            {
                System.Diagnostics.Process.Start("mailto: " + ur.NavigateUri);
            }
        }
    }

    public class Book
    {
        public int Id { get; set; }
        public string ISBN { get; set; }
        public string Name { get; set; }
        public string Summary { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
        public bool IsSold { get; set; }
        public WeekDay WorkDay { get; set; }
        public string Email { get; set; }
    }

    public enum WeekDay
    {
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6,
        Sunday = 7
    }
}

 

 

 

 

标签:set,datagridtemplatecolumn,DataGridTemplateColumn,get,System,CellEditingTemplate
From: https://www.cnblogs.com/Fred1987/p/18171617

相关文章