系列文章目录

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之OpenFileDialogWPF(11)WPF之DataGrid的CellTemplateSelector使用


本文主要记录的是在DataGrid中如何根据数据类型的不同展示不同的数据格式。

例如:

wpf TabItem header 无效_c#


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

文章目录

  • 系列文章目录
  • DataTemplateSelector
  • 数据模型
  • 定义模板选择器ParameterDataTemplateSelector
  • 定义数据模板
  • 【注】模板选择器和模板
  • 数据构造部分
  • 数据使用部分



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 自定义控件库中的,为了方便后期的重用

wpf TabItem header 无效_System_02


模板中的代码时定义在Generic.xaml中的

wpf TabItem header 无效_wpf_03

数据构造部分

wpf TabItem header 无效_System_04

数据使用部分

wpf TabItem header 无效_wpf_05


同时需要在xaml页面中引入资源文件

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

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