为控件动态添加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 ...
随机推荐
- 使用ASP.NET AJAX 从脚本中调用Web 服务的应用方法
技能点:通过编写WebService,在页面js中调用WebService来进行数据查询. 网站开发,有些时候需要使用js在页面动态生成一些内容,但还有些数据要通过查询数据库才能获取的. 但由于诸如主 ...
- TZOJ 1840 Jack Straws(线段相交+并查集)
描述 In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the ta ...
- OptionParser模块学习
from optparse import OptionParser import sys useage = [] test_parser = OptionParser(usage="%pro ...
- [leetcode]438. Find All Anagrams in a String找出所有变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- 关于block元素和inline元素
呃...这个会不会太基础了.最近在复习,所以基础知识也不能够忽略. 根据HTML 4.01 规范,其描述如下(http://www.w3.org/TR/html401/struct/global.ht ...
- Java中的NIO及IO
1.概述 Java NIO(New IO) 是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同, ...
- cmake 总结
cmake中一些预定义变量 PROJECT_SOURCE_DIR 工程的根目录 PROJECT_BINARY_DIR 运行cmake命令的目录,通常是${PROJECT_SOURCE_DIR}/bui ...
- 在Struts2框架中使用OGNL表达式(在jsp页面中使用OGNL表达式)
1. Struts2引入了OGNL表达式,主要是在JSP页面中获取值栈中的值 2. 具体在Struts2中怎么使用呢?如下步骤 * 需要先引入Struts2的标签库(在JSP页面的最上面位置) > ...
- div+css隐藏内容样式方法
div css隐藏内容样式方法 div+css隐藏内容方法 一般情况下,css隐藏的用途,如下: 1.对文本的隐藏 2.隐藏超链接(另类黑链) 3.对统计代码隐藏 4.隐藏超出图片 5.css ...
- codevs1246 丑数
题目描述 Description 对于一给定的素数集合 S = {p1, p2, ..., pK}, 来考虑那些质因数全部属于S 的数的集合.这个集合包括,p1, p1p2, p1p1, 和 p1p2 ...