代码注意事项:

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代码注意事项,开发常见问题,知识总结的更多相关文章

  1. 为企业应用开发提速,写给企业IT部门的低代码开发基础知识

    简介:应用程序开发长期以来一直是IT部门和业务部门面临的问题. IT部门总是被新的应用程序需求弄得不堪重负.他们不可能完成业务部门想要完成的每一个项目. 同时,业务部门的用户厌倦了等待,并开始完全绕过 ...

  2. VueJS 开发常见问题集锦

    由于公司的前端开始转向 VueJS,最近开始使用这个框架进行开发,遇到一些问题记录下来,以备后用. 主要写一些 官方手册 上没有写,但是实际开发中会遇到的问题,需要一定知识基础. 涉及技术栈 CLI: ...

  3. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  4. 【译】Visual Studio 2019 中 WPF & UWP 的 XAML 开发工具新特性

    原文 | Dmitry 翻译 | 郑子铭 自Visual Studio 2019推出以来,我们为使用WPF或UWP桌面应用程序的XAML开发人员发布了许多新功能.在本周的 Visual Studio ...

  5. 移动端 Web 开发前端知识整理

    文章来源: http://www.restran.net/2015/05/14/mobile-web-front-end-collections/ 最近整理的移动端 Web 开发前端知识,不定期更新. ...

  6. XBOX ONE游戏开发常见问题

    XBOX ONE游戏开发常见问题 终于弄懂这个在Unity的sdk在Account Picker切换账号的机制了,一个手柄注册一个账号,在游戏里面的时候,只有另外一个手柄选择自己的账号,系统的Acti ...

  7. WPF和Expression Blend开发实例:一个样式实现的数字输入框

    原文:WPF和Expression Blend开发实例:一个样式实现的数字输入框 今天来一个比较奇淫技巧的手法,很少人用,同时也不推荐太过频繁的使用. 先上样式: <Style x:Key=&q ...

  8. Ext常用开发基础知识

    Ext常用开发基础知识 组件定义 //这种方法可以缓存所需要的组件 调用起来比较方便(方法一 ) Ext.define('MySecurity.view.home.HomePanel', { //添加 ...

  9. 谈谈关于PHP的代码安全相关的一些致命知识

    谈谈关于PHP的代码安全相关的一些致命知识 目标 本教程讲解如何防御最常见的安全威胁:SQL 注入.操纵 GET 和 POST 变量.缓冲区溢出攻击.跨站点脚本攻击.浏览器内的数据操纵和远程表单提交. ...

随机推荐

  1. ubuntu ssh连接服务器保持长时间不断

    方法: ssh -o serveraliveinterval=60 username@ip

  2. 【Luogu】P1967货车运输(最大生成森林+倍增LCA)

    题目链接 倍增LCA是个什么蛇皮原理啊,循环完了还得再往上跳一次才能到最近公共祖先 合着我昨天WA两次就是因为这个 建最大生成森林,因为图不一定是联通的,所以不一定是一棵树.这个地方用克鲁斯卡尔就好了 ...

  3. SPOJ GSS3 Can you answer these queries III ——线段树

    [题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...

  4. 刷题总结——mokia(bzoj1176)

    题目: 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. Inp ...

  5. 刷题总结——宠物收养所(bzoj1208)

    题目: Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明 ...

  6. FreeMarker数据模板引擎全面教程mark

    http://blog.csdn.net/fhx007/article/details/7902040/#comments 以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复 ...

  7. SHoj A序列

    A序列 发布时间: 2017年7月9日 18:17   最后更新: 2017年7月9日 21:05   时间限制: 1000ms   内存限制: 128M 描述 如果一个序列有奇数个正整数组成,不妨令 ...

  8. 云计算与 OpenStack

    “云计算” 算是近年来最热的词了.现在 IT 行业见面不说这三个字您都不好意思跟人家打招呼. 对于云计算,学术界有各种定义,大家有兴趣可以百度一下. CloudMan 这里主要想从技术的角度谈谈对云计 ...

  9. Peaks BZOJ 3545 / Peaks加强版 BZOJ 3551

    Peaks [问题描述] 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问 ...

  10. android CheckBox使用和状态获得

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...