WPF代码注意事项,开发常见问题,知识总结
代码注意事项:
1、代码实现的样式赋值
XXX.Style = TryFindResource("StyleName") as Style;
2.WPF中FindName方法的使用
(1)简单的使用 前台代码:
<Button x:Name= "btnName" Click= "btnName_Click" >Test Method FindName</Button> |
后台代码:
private void btnName_Click(object sender, RoutedEventArgs e)
{
Button b = FindName("btnName") as Button;
MessageBox.Show(b.Name);
}
(2)在模板中使用FindName方法
<Grid> <Grid x:Name= "childGrid" > <Button x:Name= "rootBtn" > <Button.Template> <ControlTemplate> <Button x:Name= "btnName" Click= "btnName_Click" >Test Method FindName</Button> </ControlTemplate> </Button.Template> </Button> </Grid> </Grid> |
后台代码:
private void btnName_Click( object sender, RoutedEventArgs e) { Button b = rootBtn.Template.FindName( "btnName" ,rootBtn) as Button; MessageBox.Show(b.Name); } |
3.使用多个绑定参数
<Button Height= "23" Command= "{Binding AddCommand}" Content= "计算" HorizontalAlignment= "Left" Margin= "20,0,0,49" Name= "button1" VerticalAlignment= "Bottom" Width= "75" > <Button.CommandParameter> <MultiBinding Converter= "{StaticResource ParameterConverter}" > <Binding Path= "Text" ElementName= "textBox1" /> <Binding Path= "Text" ElementName= "textBox2" /> </MultiBinding> </Button.CommandParameter> |
4、将代码写在XAML中
<Grid> <x:Code> <![CDATA[ private void button1_Click( object sender, RoutedEventArgs e) { button1.Background = Brushes.Blue; button1.Content = "The code is in XAML" ; MessageBox.Show( "hi" ); } ]]></x:Code> <Button Height= "23" Margin= "46,56,32,0" Name= "button1" VerticalAlignment= "Top" Click= "button1_Click" >Click me!</Button> </Grid> |
5、各属性对应的C#代码
5.1<Window><Grid><Button ...></Grid></window>
等效代码如下:
Grid g = new Grid(); Button b = new Button(); b.Width = 100; b.Height = 100; b.Content = "Test Button" ; b.Foreground = Brushes.LightBlue; g.Children.Add(b); myWindow.AddChild(g); |
6、ItemContainerGenerator.ContainerFromIndex方法的使用
TreeViewItem item = (TreeViewItem)myTreeView.ItemContainerGenerator.ContainerFromIndex(1); |
7、WPF获取子控件和父控件方法
public class Globals { /// <summary> /// 获取父控件方法。该方法将根据当前控件,遍历查找其父控件是否存在。参数1是表示当前子控件名,参数2是要查询父控件名; /// </summary> public T GetParentObject<T>(DependencyObject obj, string name) where T : FrameworkElement { DependencyObject parent = VisualTreeHelper.GetParent(obj); while (parent != null ) { if (parent is T && (((T)parent).Name == name | string .IsNullOrEmpty(name))) { return (T)parent; } parent = VisualTreeHelper.GetParent(parent); } return null ; } /// <summary> /// 该方法将根据当前控件,遍历查找其子控件是否存在。参数1是表示当前父控件名,参数2是要查询子控件名; /// </summary> public T GetChildObject<T>(DependencyObject obj, string name) where T : FrameworkElement { DependencyObject child = null ; T grandChild = null ; for ( int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++) { child = VisualTreeHelper.GetChild(obj, i); if (child is T && (((T)child).Name == name | string .IsNullOrEmpty(name))) { return (T)child; } else { grandChild = GetChildObject<T>(child, name); if (grandChild != null ) return grandChild; } } return null ; } /// <summary> /// 该方法将把所有子控件作为List集合返回到客户端。 /// 其中第一个参数是父控件参数,而第二个参数是特定子控件名称, /// 如果需要遍历全部子控件,第二个参数留空即可。 /// </summary> public List<T> GetChildObjects<T>(DependencyObject obj, string name) where T : FrameworkElement { DependencyObject child = null ; List<T> childList = new List<T>(); for ( int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++) { child = VisualTreeHelper.GetChild(obj, i); if (child is T && (((T)child).Name == name || string .IsNullOrEmpty(name))) { childList.Add((T)child); } childList.AddRange(GetChildObjects<T>(child, "" )); } return childList; } } |
XAML:
Globals VTHelper = new Globals(); StackPanel sp = VTHelper.GetChildObject<StackPanel>( this .LayoutRoot, "spDemoPanel" ); Grid layoutGrid = VTHelper.GetParentObject<Grid>( this .spDemoPanel, "LayoutRoot" ); List<TextBlock> textblock = VTHelper.GetChildObjects<TextBlock>( this .LayoutRoot, "" ); |
8.GridSplitter控件的使用:
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width= "90*" /> <ColumnDefinition Width= "Auto" /> <ColumnDefinition Width= "180*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height= "190*" /> <RowDefinition Height= "Auto" /> <RowDefinition Height= "70*" /> </Grid.RowDefinitions> <Button Content= "ButtonA" Margin= "3" Grid.Row= "0" Grid.Column= "0" Grid.RowSpan= "3" /> <Button Content= "ButtonB" Margin= "3" Grid.Row= "0" Grid.Column= "2" /> <Button Content= "ButtonC" Margin= "3" Grid.Row= "2" Grid.Column= "2" /> <GridSplitter Width= "3" HorizontalAlignment= "Stretch" VerticalAlignment= "Stretch" Grid.Row= "0" Grid.Column= "1" Grid.RowSpan= "3" ></GridSplitter> <GridSplitter Height= "3" VerticalAlignment= "Stretch" HorizontalAlignment= "Stretch" Grid.Row= "1" Grid.Column= "2" ></GridSplitter> </Grid> |
效果图:
9.UniformGrid控件中每行没列都相等
<UniformGrid> <Button Content= "ButtonA" /> <Button Content= "ButtonB" /> <Button Content= "ButtonC" /> <Button Content= "ButtonD" /> <Button Content= "ButtonE" /> <Button Content= "ButtonF" /> <Button Content= "ButtonG" /> <Button Content= "ButtonH" /> </UniformGrid> |
如图:
http://www.cnblogs.com/linlf03/archive/2011/09/07/2159169.html
WPF代码注意事项,开发常见问题,知识总结的更多相关文章
- 为企业应用开发提速,写给企业IT部门的低代码开发基础知识
简介:应用程序开发长期以来一直是IT部门和业务部门面临的问题. IT部门总是被新的应用程序需求弄得不堪重负.他们不可能完成业务部门想要完成的每一个项目. 同时,业务部门的用户厌倦了等待,并开始完全绕过 ...
- VueJS 开发常见问题集锦
由于公司的前端开始转向 VueJS,最近开始使用这个框架进行开发,遇到一些问题记录下来,以备后用. 主要写一些 官方手册 上没有写,但是实际开发中会遇到的问题,需要一定知识基础. 涉及技术栈 CLI: ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- 【译】Visual Studio 2019 中 WPF & UWP 的 XAML 开发工具新特性
原文 | Dmitry 翻译 | 郑子铭 自Visual Studio 2019推出以来,我们为使用WPF或UWP桌面应用程序的XAML开发人员发布了许多新功能.在本周的 Visual Studio ...
- 移动端 Web 开发前端知识整理
文章来源: http://www.restran.net/2015/05/14/mobile-web-front-end-collections/ 最近整理的移动端 Web 开发前端知识,不定期更新. ...
- XBOX ONE游戏开发常见问题
XBOX ONE游戏开发常见问题 终于弄懂这个在Unity的sdk在Account Picker切换账号的机制了,一个手柄注册一个账号,在游戏里面的时候,只有另外一个手柄选择自己的账号,系统的Acti ...
- WPF和Expression Blend开发实例:一个样式实现的数字输入框
原文:WPF和Expression Blend开发实例:一个样式实现的数字输入框 今天来一个比较奇淫技巧的手法,很少人用,同时也不推荐太过频繁的使用. 先上样式: <Style x:Key=&q ...
- Ext常用开发基础知识
Ext常用开发基础知识 组件定义 //这种方法可以缓存所需要的组件 调用起来比较方便(方法一 ) Ext.define('MySecurity.view.home.HomePanel', { //添加 ...
- 谈谈关于PHP的代码安全相关的一些致命知识
谈谈关于PHP的代码安全相关的一些致命知识 目标 本教程讲解如何防御最常见的安全威胁:SQL 注入.操纵 GET 和 POST 变量.缓冲区溢出攻击.跨站点脚本攻击.浏览器内的数据操纵和远程表单提交. ...
随机推荐
- [android开放篇] wifi-direct接口网址
http://www.android-doc.com/guide/topics/connectivity/wifip2p.html
- .NET重构(九):机房重构验收总结
导读:机房收费系统个人重构版,在寒假前,已经结束了.嗯,这一路的过程,也挺心酸的.结合师傅验收时的指导.建议,对这一段时间的学习,进行一个总结. 一.学习过程 这一阶段的学习,按照师傅给的建议是:由浅 ...
- NOJ——1628Alex’s Game(III)(DFS+回溯)
[1628] Alex’s Game(III) 时间限制: 1000 ms 内存限制: 65535 K 问题描述 Alex likes to play with one and zero as you ...
- BZOJ3122 [Sdoi2013]随机数生成器 【BSGS】
题目 输入格式 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数. 接下来T行,每行有五个整数p,a,b,X1,t,表示一组数据.保证X1和t都是合法的页码. 注意:P一定为质数 输出 ...
- bzoj1063【Noi2008】道路设计
题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1063 用一种划分方式将树划为重链和轻链,使得所有点到根节点的路径经过的轻链最大值最小 sol: ...
- 刷题总结——随机图(ssoi)
题目: 随机图 (random.cpp/c/pas) [问题描述] BG 为了造数据,随机生成了一张�个点的无向图.他把顶点标号为1~�. 根据BG 的随机算法,对于一个点对�, �(1 ≤ � &l ...
- cf396B On Sum of Fractions
Let's assume that v(n) is the largest prime number, that does not exceed n; u(n) is the smallest pri ...
- STL学习笔记(一) 容器
0.前言随机访问迭代器: vector.string.dequeSTL的一个革命性的方面就是它的计算复杂性保证 条款01:慎重选择容器类型 c++提供的容器:标准STL序列容器:vector.stri ...
- 洛谷 P1756 最小花费
题目背景 题目描述 在n个人中,某些人的银行账号之间可以互相转账.这些人之间转账的手续费各不相同.给定这些人之间转账时需要从转账金额里扣除百分之几的手续费,请问A最少需要多少钱使得转账后B收到100元 ...
- HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】
Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K ...