WPF (DataGridColumnHeader)实现自义定列头样式 并绑定数据

本文介绍了一种在WPF中为DataGrid的列头自定义复杂样式的实现方法,包括如何通过后台循环添加列头,并详细解释了自定义控件LableColumn的创建过程,以实现对列头数据的灵活处理和绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

实现功能是这样的 

自定义列头 样式 样式里的 数据来源于后台绑定 

这篇就说头样式 和头样式数据绑定

思路

1)实现功能的时候 首先想的是编辑列头样式 选择使用DataGridTextColumn 编辑 DataGridColumnHeader 样式

样式很简单 就布局好了 这段结束

2)动态列 没有要求换肤 所以就没有完全使用MVVM 直接写后台循环   到这里数据有了

      List<string> LS = new List<string>();

        public void addColumn() 
        {
            LS.Add("表下カップ綿天竺仮縫い_37s_C_1");
            LS.Add("上カップマーキしつけ_28s_C_2");
            LS.Add("上下カップ接ぎ_33s_C_3");
            LS.Add("上下カップ押え_62s_B_4");
            LS.Add("カップ脇しつけ_14s_B_5");
            LS.Add("表上カップレース端押さえ_41s_B_6");

            for (int i = 0; i < LS.Count; i++)
			{ 
                 DataGridTextColumn dl = new DataGridTextColumn();
                dl.Header=LS[i];
          
                dataGrid.Columns.Add(dl);
			}
        
        }

3)最难的数据绑定 数据来源 header 如果有只有俩个 就不用那么麻烦 直接在样式里ControlTemplate   中用TemplateBinding 绑定 Content 和tag  就可以

{TemplateBinding Content}

content = Header 里的值  当然 要使用tag 就要在上面的for 里加上tag的值 样式里 需要 绑定{TemplateBinding tag}

但是 我的项目需要4个 这就需要夸越TemplateBinding 这个绑定 我查了一下 想扩展template 但是资料太少 

解决方法  自义定控件

首先我显示的控件是lable 所以 我自定义了一个lable 写 依赖属性 虽然有点繁琐 也算是一个比较笨的解决方案

1)定义 Ms  来获得header的数据  并处理数据 

2)定义MyProperty 来获得Ms处理后的数据 绑定到 lable 的 Content 属性

3)使用控件本身的tag 来区分那个lable

 贴码:

自定义的lable控件

public class LableColumn : Label
    {
        public LableColumn()
            : base() 
        {
        }

        //获得值
        public string Ms    
        {
            get { return (string)GetValue(MsProperty); }
            set { SetValue(MsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ms.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MsProperty =
            DependencyProperty.Register("Ms", typeof(string), typeof(LableColumn), new FrameworkPropertyMetadata("", Onshow));

 

        //用于绑定的值
        public string MyProperty
        {
            get { return (string)GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(string), typeof(LableColumn), new PropertyMetadata(""));

        
 
        

        private static void Onshow(DependencyObject d, DependencyPropertyChangedEventArgs e) 
        {
            LableColumn l = d as LableColumn;

            if (l.Ms != null && l.Ms != "")
            {
                String[] strarr = l.Ms.ToString().Split(new string[] { "_" }, StringSplitOptions.None);
           
                if (l.Tag.Equals("1"))
                {
                 l.MyProperty=   strarr[3];
                }
                else if (l.Tag.Equals("2"))
                {
                    l.MyProperty = strarr[0];
                 
                }
                else if (l.Tag.Equals("3"))
                {  l.MyProperty= strarr[2] ;  
                }
                else if (l.Tag.Equals("4"))
                {
                     l.MyProperty= strarr[1];
                }
            }
        }
 

    }

前台的DataGridColumnHeader 样式

  <Style TargetType="{x:Type DataGridColumnHeader}">
			<Setter Property="VerticalContentAlignment" Value="Center"/>
			<Setter Property="Template">
				<Setter.Value>
					<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">

                        <Grid HorizontalAlignment= "Left " Height= "Auto " VerticalAlignment= "Stretch " Width= "Auto " Background= "White " Margin= "0 ">
                            <Grid.RowDefinitions>
                                <RowDefinition Height= "* "/>
                            </Grid.RowDefinitions>
                            <Border BorderBrush= "Black " BorderThickness= "1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Grid.RowSpan= "1 ">
                                <Grid HorizontalAlignment= "Stretch " Height= "Auto " Margin= "-1,0,0,0 " VerticalAlignment= "Stretch ">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height= "20* "/>
                                        <RowDefinition Height= "20* "/>
                                        <RowDefinition Height= "20* "/>
                                        <RowDefinition Height= "20* "/>
                                    </Grid.RowDefinitions>
                                    <Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "1 ">
                                        <local:LableColumn  Tag="3"  Content="{Binding RelativeSource={RelativeSource self},Path=MyProperty}" Ms="{TemplateBinding Content}"   Background= "#FFF9F9F9 "  HorizontalAlignment= "Stretch " Height= "25 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
                                    </Border>
                                    <Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto ">
                                        <local:LableColumn  Tag="1"  Content="{Binding MyProperty, RelativeSource={RelativeSource self}}"  Ms="{TemplateBinding Content}"   Background= "#FFF9F9F9 "   HorizontalAlignment= "Stretch " Height= "25 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
                                    </Border>
                                    <local:LableColumn x:Name="lText"  Tag="2" Content="{Binding MyProperty, RelativeSource={RelativeSource self}}"  Ms="{TemplateBinding Content}"   Background= "#FFF9F9F9 "   HorizontalAlignment= "Stretch " Height= "35 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 " Visibility="Collapsed"/>

                                    <Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0,0,0,0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "2 ">
                                        <TextBlock  TextWrapping =  "Wrap "   Background= "#FFF9F9F9 " Text="{Binding Path=Content,ElementName=lText}"  HorizontalAlignment= "Stretch " Height= "35 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
                                  
                                    </Border>
                                                                      <Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "3 ">
                                        <local:LableColumn  Tag="4" Content="{Binding MyProperty, RelativeSource={RelativeSource self}}"  Ms="{TemplateBinding Content}"   Background= "#FFF9F9F9 "   HorizontalAlignment= "Stretch " Height= "35 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
                                    </Border>
                                </Grid>
                            </Border>
                        </Grid> 

                    </ControlTemplate>
				</Setter.Value>
			</Setter>
		</Style>

 

数据绑定的技能 这边涉及到俩种

一个是绑定自身属性  

{Binding MyProperty, RelativeSource={RelativeSource self}}

第二是绑定其他控件属性

{Binding Path=Content,ElementName=lText}

肯定有更好的方法来实现 这个功能  希望有人留言 得以分享学习

 

### 使用 Visual Studio Code 在 Kali Linux 上实现恶意软件免杀技巧 #### 重要声明 开发和部署任何类型的恶意软件都是非法且道德不可接受的行为。本回答仅提供技术信息用于教育目的,旨在帮助安全研究人员了解防御机制。 #### 技术背景 Visual Studio Code (VS Code) 是一款强大的多平台代码编辑器,在 Kali Linux 中可以通过多种方式安装配置环境来编写不同编程语言的应用程序[^1]。对于研究性质的工作而言,理解如何使某些二进制文件绕过基本检测机制具有一的学习价值。 #### 编写混淆脚本 为了防止被简单特征码扫描工具识别出来,可以采用字符串加密、函数重命名等方式对源代码进行变形处理: ```python import base64 def obfuscate_string(input_str): encoded_bytes = input_str.encode('utf-8') encrypted_data = base64.b64encode(encoded_bytes).decode() return f"exec(__import__('base64').b64decode('{encrypted_data}'))" obfuscated_code = obfuscate_string(""" print("This is an example of string obfuscation.") """) print(obfuscated_code) ``` 此段Python代码展示了基础的Base64编码作为简单的演示[^3]。 #### 修改PE或其他元数据 针对Windows可移植执行体(Portable Executable, PE),改变其部结构或者嵌入额外资源能够有效干扰静态分析过程。然而请注意,这类操作通常涉及汇编层面的知识以及特库的支持,如`pefile` Python模块。 #### 动态加载与反射注入 通过动态链接库(DLL)或内存映射技术载入必要的功能组件而非直接包含于主程序体内;这种方法增加了逆向工程难度的同时也使得传统的基于签名的安全产品难以捕捉到完整的攻击模式[^5]。 #### 创建自打包方案 利用PyInstaller等工具将解释型语言转换成独立运行的原生应用,加入加壳保护措施进一步增强隐蔽效果。不过需要注意的是,过度复杂的封装可能会引起高级防护系统的怀疑。 #### 配置 VS Code 支持上述工作流程 确保已按照官方指南完成VS Code及其扩展插件的设置,以便支持目标编程语言特性及调试需求[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小慧哥

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值