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 变量.缓冲区溢出攻击.跨站点脚本攻击.浏览器内的数据操纵和远程表单提交. ...
随机推荐
- c语言头文件以及make注意事项
c语言头文件以及make注意事项 头文件说明:自己定义的头文件和项目文件放在一起,注意使用""而不是使用<>,系统的头文件才使用<> 当main函数要调用其 ...
- POJ-1696 Space Ant 凸包版花式水过!
Space Ant 明天早上最后一科毛概了,竟然毫无复习之意,沉迷刷题无法自拔~~ 题意:说实 ...
- [UOJ#130][BZOJ4198][Noi2015]荷马史诗
[UOJ#130][BZOJ4198][Noi2015]荷马史诗 试题描述 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静 ...
- java system.out.printf()的使用方法
package test; public class Main { public static void main(String[] args) { // 定义一些变量,用来格式化输出. double ...
- Vue && Angular 双向绑定检测不到对象属性的添加和删除
由于ES5的限制 Vue && Angular 双向绑定检测不到对象属性的添加和删除 还有数组增加索引.这些改变不会触发change事件.Vue是因为实例化的时候已经把各个属性都s ...
- 路飞学城详细步骤 part2
一 显示课程列表 需求:当你点击课程,course.vue在 <router-view>渲染,并不需要你进行其他点击,所欲的课程列表直接在前端显示,数据是从数据库拿到的. 补充1:生命周期 ...
- PHP提示Cannot modify header information - headers already sent by解决方法
PHP提示Cannot modify header information - headers already sent by解决方法 因为 header();发送头之前不能有任何输出,空格也不行, ...
- Yii 之视图布局
控制器代码: //设置的布局文件 public $layout = 'common'; public function actionAbout(){ $data = array('page_name' ...
- P1551 亲戚 洛谷
https://www.luogu.org/problem/show?pid=1551 题目背景 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个 ...
- 纯Java实现定时任务(转)
第一种: import java.util.Timer; import java.util.TimerTask; /** * * 于第一种方式相比,优势 1>当启动和去取消任务时可以控制 2&g ...