WPF(11)WPF之DataGrid的CellTemplateSelector使用

系列文章目录

WPF(1)WPF工程创建
WPF(2)WPF之Image组件
WPF(3)WPF之Button组件
WPF(5)WPF之DataGrid控件
WPF(6)WPF之TextBox控件
WPF(7)WPF之ProgressBar控件
WPF(8)WPF之GroupBox控件(登录界面示例)
WPF(9)WPF之常用弹窗的工具类
WPF(10)WPF之OpenFileDialog
WPF(11)WPF之DataGrid的CellTemplateSelector使用


本文主要记录的是在DataGrid中如何根据数据类型的不同展示不同的数据格式。
例如:
在这里插入图片描述
在此项目数据格式可能是TextBox,可能是CheckBox 也可能是ComboBox.以上的数据展示格式都是为了能够方便用户的使用,问题的根就在这里,如何让程序通过数据帮我们选择不同的控件,也就是展示数据的方式呢?数据中是通过读取项目文档获取的,至于有缩多少参数,参数是什么类型的都要自己取获取并解析。本文对于数据解析处理的记录的内容不是很多。主要是处理如何展示数据。


DataTemplateSelector

数据模板选择器是本文的核心,所有的内容都是围改内容展开的。顾名思义,就是通过数据帮助我我们选择模板。这也是我们问题的根源。

数据模型

我们先根据数据展示的类型定制不同的数据模板。对于模板数据的共有部分进行了父类的封装。
父类BasePModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using HuaSheCityRoadBridge.DataContracts.Enums;

namespace HuaSheCityRoadBridge.DataContracts.ParameterObjects
{
    public class BasePModel:ObservableObject
    {
        public DataSourceType DataSourceType { get; set; } = DataSourceType.Temporary;
        public string ParameterType { get; set; }
        public string TypeName { get; protected set; }

        private long id;
        public long Id
        {
            get { return id; }
            set { id = value; }
        }

        private string guid;

        public string Guid
        {
            get { return guid; }
            set { guid = value; }
        }
        private string displayname;
        public string Displayname
        {
            get
            {
                return displayname.Trim(new char[] { '_' });
            }
            private set
            {
                displayname = value;
                RaisePropertyChanged();
            }
        }
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {

                name = value;
                Displayname = name;
                RaisePropertyChanged();
            }
        }
    }
}

子类TextBoxPModel封装

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using HuaSheCityRoadBridge.DataContracts.Enums;

namespace HuaSheCityRoadBridge.DataContracts.ParameterObjects
{
    public class TextBoxPModel :BasePModel
    {
        public TextBoxPModel()
        {
            TypeName = "TextBox";
        }
       
        private string content;
        public string Content
        {
            get { return content; }
            set { content = value; RaisePropertyChanged(); }
        }

    }
}

子类ComboBoxPModel封装

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using HuaSheCityRoadBridge.DataContracts.Enums;

namespace HuaSheCityRoadBridge.DataContracts.ParameterObjects
{
    public class ComboBoxPModel : BasePModel
    {
       
        public ComboBoxPModel()
        {
            TypeName = "ComboBox";
        }
        private ObservableCollection<object> itemSource;

        public ObservableCollection<object> ItemSource
        {
            get { return itemSource; }
            set { itemSource = value; RaisePropertyChanged(); }
        }

        private object selectedItem;

        public object SelectedItem
        {
            get { return selectedItem; }
            set { selectedItem = value; RaisePropertyChanged(); }
        }
    }
}

子类CheckBoxPModel封装

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using HuaSheCityRoadBridge.DataContracts.Enums;

namespace HuaSheCityRoadBridge.DataContracts.ParameterObjects
{
    public class CheckBoxPModel :BasePModel
    {
        public CheckBoxPModel()
        {
            TypeName = "CheckBox";
        }

        private bool isCheck;

        public bool IsCheck
        {
            get { return isCheck; }
            set { 
                isCheck = value;
                RaisePropertyChanged();
                intValue = isCheck ? 1 : 0;
            }
        }

        private int intValue;

        public int IntValue
        {
            get { return intValue; }
            set
            {
                intValue = value;
                IsCheck = intValue == 0;
            }
        }
    }
}

定义模板选择器ParameterDataTemplateSelector

该类继承了DataTemplateSelector,并实现了SelectTemplate虚方法。通过该虚方法,根据我们每一个控件设置的TypeName去选择合适的模板类型。

using HuaSheCityRoadBridge.DataContracts.ParameterObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace HuaSheCityRoadBridge.UIControl.TemplateSelectors
{
    public class ParameterDataTemplateSelector : DataTemplateSelector
    {
        public DataTemplate TemplateTextBox { get; set; }
        public DataTemplate TemplateCheckBox { get; set; }
        public DataTemplate TemplateComboBox { get; set; }
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item is BasePModel)
            {
                BasePModel basePModel = item as BasePModel;
                switch (basePModel.TypeName)
                {
                    case "TextBox":
                        return TemplateTextBox;
                    case "CheckBox":
                        return TemplateCheckBox;
                    case "ComboBox":
                        return TemplateComboBox;
                    default:
                        return null;
                }
            }
            return null;
            
        }
    }
}

定义数据模板

以下的内容使用的是WPF 资源字典,名称未Generic.xaml。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:pdt="clr-namespace:HuaSheCityRoadBridge.UIControl.TemplateSelectors">

    <pdt:ParameterDataTemplateSelector x:Key="ParameterDataTemplateSelector">
        <pdt:ParameterDataTemplateSelector.TemplateTextBox>
            <DataTemplate>
                <TextBox Text="{Binding Content}" Width="90" IsReadOnly="False"/>
            </DataTemplate>
        </pdt:ParameterDataTemplateSelector.TemplateTextBox>
        <pdt:ParameterDataTemplateSelector.TemplateCheckBox>
            <DataTemplate>
                <CheckBox  Width="90"
                           VerticalAlignment="Center"
                           IsChecked="{Binding IsChecked}" />
            </DataTemplate>
        </pdt:ParameterDataTemplateSelector.TemplateCheckBox>
        <pdt:ParameterDataTemplateSelector.TemplateComboBox>
            <DataTemplate>
                <ComboBox Width="90"
                          VerticalContentAlignment="Center"
                          ItemsSource="{Binding ItemSource}"
                          SelectedItem="{Binding SelectedItem}"
                          DisplayMemberPath="Name" />
            </DataTemplate>
        </pdt:ParameterDataTemplateSelector.TemplateComboBox>
    </pdt:ParameterDataTemplateSelector>
</ResourceDictionary>

【注】模板选择器和模板

以上的两部分内容是定义在新建的WPF 自定义控件库中的,为了方便后期的重用

在这里插入图片描述
模板中的代码时定义在Generic.xaml中的
在这里插入图片描述

数据构造部分

在这里插入图片描述

数据使用部分

在这里插入图片描述
同时需要在xaml页面中引入资源文件

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/HuaSheCityRoadBridge.UIControl;Component/Themes/Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

以上的代码就是解决此问题的所有内容了。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yy763496668

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值