为控件动态添加Style
此文可解决: 重写控件时,给控件加入子控件或父控件的样式切换问题。 很灵活的可以根据不同内容显示不同样式
子控件作用在:
<DataTemplate x:Key="ColmunHeader1">
<DockPanel Background="Transparent">
<!-- The control to host the filter UI for this column -->
<controls:dgDataGrid DockPanel.Dock="Right" />
<ContentPresenter x:Name="PART_Content" Content="{Binding}"
SnapsToDevicePixels="{Binding SnapsToDevicePixels, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}"
HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}"
VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}" />
</DockPanel>
</DataTemplate>
如下样式:
<ControlTemplate x:Key="DataGridCheckBoxColumn1">
<Grid>
<CheckBox
/>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="DataGridTextBoxColumn1">
<Grid>
<TextBox/>
</Grid>
</ControlTemplate>
然后在子控件类的loaded事件中加入:
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.Controls.Primitives;
using System.Windows.Media; namespace WpfApp1.Controls
{
public class dgDataGrid : Control
{
public DataGridColumnHeader ColumnHeader { get; set; } public dgDataGrid()
{
this.Loaded += (s, e) =>
{
ColumnHeader = this.FindAncestorOrSelf<DataGridColumnHeader>();
var value = ColumnHeader.Column.GetType();
if (value == typeof(DataGridTextColumn))
{
this.Template = this.FindResource("DataGridTextColumn1") as ControlTemplate;
}
else
{
this.Template = this.FindResource("DataGridCheckBoxColumn1") as ControlTemplate;
}
};
}
}
}
为控件动态添加Style的更多相关文章
- C# DataGridView控件 动态添加新行
DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如需要动态为DataGridView控件添加新行, ...
- C# DataGridView控件动态添加新行
C# DataGridView控件动态添加新行 DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如 ...
- DotNetBar RibbonControl 控件动态添加项
想做个插件式开发,界面用Dotnetbar的RibbonControl,需要通过代码动态的向RibbonControl控件添加项 示例代码如下: RibbonTabItem rti = new Rib ...
- Android_(控件)动态添加或删除Spinner下拉菜单
使用ArrayList动态数组的依赖性实现动态增减Spinner下拉菜单选项功能. 设置一个EditText输入框,当用户输入了文字并单击[添加]按钮的同时,就会将输入的值添加Spinner至下拉菜单 ...
- duilib 修复CTreeViewUI控件动态添加子控件时,对是否显示判断不足的bug
转载请说明出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42264947 这个bug我在仿酷狗开发日志里提到过,不过后来发现修复的不够 ...
- js给div动态添加控件,然后给这个控件动态添加事件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- C#-将控件动态添加到选项卡页tablepage
tabPage1.Controls.Add(new Button()); 实例: Button cp = new Button(); cp.text="test";cp.Click ...
- 高德地图——控件的添加&删除
控件属性 visible //bool 默认true ov=new AMap.OverView(); ov.hide(); //ov.show(); 显示/隐藏---表示控件的添加与删除 <!D ...
- wpf的UserControl用户控件怎么添加到Window窗体中
转载自 http://www.cnblogs.com/shuang121/archive/2013/01/09/2853591.html 我们来新建一个用户控件UserControl1.xaml &l ...
随机推荐
- IIS PHP Warning: Unknown: open(c:\\php\\tmp\\sess_xxx, O_RDWR) failed: Permission denied (13) in Unknown on line 0
出现这个问题的原因是你修改了php保存session的路径,但没有给用户访问该目录的权限. 右键该目录,给users用户组读.写的权限.
- sql优化常用命令总结
1.显示执行计划的详细步骤 SET SHOWPLAN_ALL ON; SET SHOWPLAN_ALL OFF; 2. 显示执行语句的IO成本,时间成本 SET STATISTICS IO ON SE ...
- APScheduler 浅析
前言 APScheduler是python下的任务调度框架,全程为Advanced Python Scheduler,是一款轻量级的Python任务调度框架.它允许你像Linux下的Crontab那样 ...
- Golang之定时器,recover
滴答滴答……定时器的使用 package main import ( "fmt" "time" ) //定时器的使用 func main() { t := ti ...
- 面向对象设计模式纵横谈:Prototype 原型模式(笔记记录)
有一段时间没写东西了,今天继续把没写完的设计模式写完,今天这堂课是创建型设计模式的最后一堂课,原型设计模式,它同样也是解决了对象在创建的过程中的解耦合的情况,面对变化使代码更稳定,更准确的说是使 ...
- UI设计不就是画线框,凭什么年薪30W?
作为一枚界面设计师 我真的很想为UI设计抱不平啊!! UI设计真是一个备受不解的职业 常会被误解,然后出现以下场景 程序欧巴: 界面画好没?按钮圆的方的不都能用吗?纠结那多干嘛? 产品经理: 这次我们 ...
- wdk Windows驱动开发
https://www.cnblogs.com/liaoguifa/p/9049859.html 安装wdk8.1
- Java NIO系列教程(十)DatagramChannel
Java NIO系列教程(十)DatagramChannel 转载自并发编程网 – ifeve.com,本文链接地址: Java NIO系列教程(十) Java NIO DatagramChannel
- 移动文件读/写指针----lseek
头文件:#include<sys/types.h>.#include<unistd.h> 函数原型:off_t lseek(int fildes,off_t offset,in ...
- $.fn.extend 和$.extend函数
区别和详解:jQuery extend()和jQuery.fn.extend() 首先是简单的概述区别:$.extend()是类方法 $.fn.extend()是原型方法 对象方法和原 ...