首页 > 其他分享 >WPF Datagrid DataGridComboBoxColumn ObjectDataProvider ObjectDataProvider.MethodParameters

WPF Datagrid DataGridComboBoxColumn ObjectDataProvider ObjectDataProvider.MethodParameters

时间:2024-05-03 20:55:57浏览次数:21  
标签:Windows Media System Datagrid ObjectDataProvider using WPF MainWindow

//xaml
<Window x:Class="WpfApp90.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:WpfApp90"
        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">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Id">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock FontSize="30" Foreground="Red" FontWeight="ExtraBold"
                                   Text="{Binding Id}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" Foreground="Blue"
                                    FontSize="30" FontWeight="ExtraBold"/>
                <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 WpfApp90
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InitCollection();
        }

        private void InitCollection()
        {
            var workersList = new List<Worker>();
            for(int i=0;i<1000;i++)
            {
                Worker wk = new Worker();
                wk.Id = i + 1;
                wk.Name = $"Name_{i + 1}";
                wk.WorkDay = GetWeekDayViaValue((i + 1) % 7);
                workersList.Add(wk);
            }
            DataContext = workersList;
        }

        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;
        }
    }

    public class Worker
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public WeekDay WorkDay { get; set; }
    }

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

 

 

 

 

标签:Windows,Media,System,Datagrid,ObjectDataProvider,using,WPF,MainWindow
From: https://www.cnblogs.com/Fred1987/p/18171597

相关文章

  • WPF上位机 - 使用转换器实现TIA Wincc中的文本列表功能
    TIAwincc中可以根据变量的值,显示出定义的文本。在WPF中可以通过转换器实现。使用哈希表存储变量和文本,根据变量值返回对应的文本显示在View中usingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Linq;usingSystem.Text;usingSy......
  • WPF上位机 - 使用转换器实现TIA Wincc中的位控制可见性或外观功能
    在TIAWincc中有这样的功能,使用Trueorfalse控制控件的可见性或者外观的情况。在上位机中需要使用转换器这样对Trueorfalse值转换为需要的笔刷或者Visible属性。usingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Linq;using......
  • WPF上位机 - S7.NETPlus批量读取数据
    在编写上位机程序中,需要读取PLC数据。使用到了S7.NETPlus这个库。S7.NETPlus提供了很多读取和写入数据的方式。大批量读取数据的方式包括一下几个方法ReadbytesReadClassReadStructReadMultipleVarsPLC侧的数据是一个Array的UDT数据,其中UDT中还包含了很多的UDT。在使用库......
  • WPF上位机 - 轴运动控制
    最近学习WPF,写了一个WPF上位机使用S7.NETPlus库与西门子1500TPLC,控制西门子伺服的通用上位机界面。分享在写上位机过程中踩的一些坑和使用体验。上位机介绍可以看到上位机分为3个区域轴选择,使能区域控制参数设置区域诊断区域选择使能区域选择区域读取TIA中组态的轴工艺......
  • WPF TreeView HierarchicalDataTemplate
    //xaml<Windowx:Class="WpfApp87.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF Text MultiBinding StringFormat
    <TextBlock.Text><MultiBindingStringFormat="R:{0:N0},G:{1:N0},B:{2:N0}"><BindingPath="Value"ElementName="_red"/><BindingPath="Value"ElementName="_green"/>......
  • WPF MultiBinding
    <Windowx:Class="WpfApp84.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF Slider Show integral value TickFrequency="1" IsSnapToTickEnabled="True"
    <Windowx:Class="WpfApp85.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF CollectionViewSource GroupDescriptions GroupStyle ItemsPanelTemplate
    <Windowx:Class="WpfApp83.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF CollectionViewSource GroupDescriptions PropertyGroupDescription GroupStyle
    <Windowx:Class="WpfApp83.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......