博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF系列四
阅读量:6878 次
发布时间:2019-06-26

本文共 7137 字,大约阅读时间需要 23 分钟。

继续学习wpf的系列课程.

首先我们看一下后台代码访问前台的UI控件.

这块的知识比较老套,不管我们开发cs,bs都一直在使用的.在讲着系列课程的时候,我以及把所以的代码,要要讲的内容都写了一遍,发博客只是改动一些东西.

<Window x:Class="WPFUserPanel.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="后天获取XAML 元素" Height="395" Width="395" BorderThickness="5" FlowDirection="LeftToRight" Grid.IsSharedSizeScope="False" ShowActivated="False" Background="BlanchedAlmond">

    <Window.Resources >

  </Window.Resources>

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition />

            <RowDefinition />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="121*" />

            <ColumnDefinition Width="123*" />

            <ColumnDefinition Width="119*" />

        </Grid.ColumnDefinitions>

        <TextBox Grid.ColumnSpan="3" Margin="27,47,31,78" Name="textBox1" FontSize="24">Hello  WPF 太神奇了</TextBox>

        <Button Grid.Row="1" Height="48" Margin="105,0,108,0" Name="button1" VerticalAlignment="Top" Click="button1_Click" Background="SandyBrown" BorderThickness="1" FontSize="18" Grid.ColumnSpan="3">第一种方式</Button>

        <Button Margin="105,54,108,76" Name="button2" Grid.Row="1" Click="button2_Click" Background="SandyBrown" BorderThickness="1" FontSize="18" Grid.ColumnSpan="3">第二种方式</Button>

        <Button Margin="105,0,108,28" Name="button3" Grid.Row="1" Click="button3_Click" Background="SandyBrown" BorderThickness="1" FontSize="18" Grid.ColumnSpan="3" Height="42" VerticalAlignment="Bottom">第三种方式</Button>

    </Grid>

</Window>

前台的xaml代码,没什么好说的.

后台代码的几种访问方式.

namespace WPFUserPanel {

    public partial class Window1 : Window {

        public Window1() {

            InitializeComponent();

        }

        void B_SizeChanged(object sender, SizeChangedEventArgs e) {

            throw new NotImplementedException();

  }

 

        private void button1_Click(object sender, RoutedEventArgs e) {

//第一种访问的方式,刚我们可以用GetValue的方式来访问,当然你直接textBox1.Text是最简单的

            System.Windows.MessageBox.Show("" + this.textBox1.GetValue(TextBox.TextProperty).ToString());

        }

 

        private void button2_Click(object sender, RoutedEventArgs e) {

//这个类似于开发bs的FindControl一样,来查找我们需要的控件

            TextBox t = this.FindName("textBox1") as TextBox;

            System.Windows.MessageBox.Show("第二种方式:" + t.Text);

        }

 

        private void button3_Click(object sender, RoutedEventArgs e) {

//这种比较麻烦, 我们先找到内容

            var v = this.Content;

//转为grid布局控件,然后把控件全部拷贝到控件数组

            Grid g = v as Grid;

            UIElementCollection uc = g.Children;

            Control[] us = new Control[uc.Count];

            g.Children.CopyTo(us, 0);

//用linq来查找这个控件. 相信大家能看懂简单的linq, 我的linq水平也是半吊子.. 一直都是在用linqToObjects,别的也没看,看20分钟 就能简单使用了.

            string str = (us.First (a => a.Name == "textBox1") as TextBox).Text;

            System.Windows.MessageBox.Show("第三种方式:" + str);

        }

    }

}

我们接着玩一个纯cs文件的wpf程序,相信大家做cs的时候,经常这样玩,我以前就是,不用窗体,只用代码来写cs程序.

我们来创建一个wpf应用程序,把所有的东西全部删掉.ok,我们把下面的代码添加进去

 

namespace WPFLayoutDemo

{

    class subMain : System.Windows.Application

    {

        [STAThread]//指定com的线程模型是单线程基元

        public static void Main()

        {

 

            System.Windows.Application app = new Application();

            MyWindow mw = new MyWindow();

            mw.Width = 400;

  mw.Height = 400;

            mw.BorderThickness = new Thickness(50, 5, 50, 5);

            app.Run(mw);//启动应用程序

        }

    }

 

    public partial class MyWindow : Window

    {

        Canvas canv;

        Ellipse e1;

        Button b1;

        Label lab1;

        Rectangle r1;

        public MyWindow()

        {

            canv = new Canvas();

            canv.Name = "C1";

            this.Content = canv;//让当前的对象等于canvas

            canv.Margin = new Thickness(0, 0, 0, 0);

            canv.Background = new SolidColorBrush(Colors.White);

 

            e1 = new Ellipse();

            e1.Fill = new SolidColorBrush(Colors.YellowGreen);

            e1.Stroke = new SolidColorBrush(Colors.Azure);

            e1.Width = 200;

            e1.Height = 200;

            e1.Margin = new Thickness(50, 100, 0, 0);

            canv.Children.Add(e1);//让canvas的实例去添加一个元素

 

 

            r1 = new Rectangle();

            r1.Fill = new SolidColorBrush(Colors.Tomato);

            r1.Opacity = 0.5;

            r1.Stroke = new SolidColorBrush(Colors.Red);

            r1.Width = 200;

            r1.Height = 200;

 

            r1.SetValue(Canvas.LeftProperty, (double)150);

            r1.SetValue(Canvas.TopProperty, (double)100);

            canv.Children.Add(r1);

 

 

            b1 = new Button();

            b1.Width = 100;

  b1.Height = 20;

            b1.Content = "修改圆形位置";

            b1.SetValue(Canvas.LeftProperty, (double)r1.GetValue(Canvas.LeftProperty) + 50);

            b1.SetValue(Canvas.TopProperty, (double)r1.GetValue(Canvas.TopProperty) + 50);

            b1.Click += new RoutedEventHandler(b1_Click);

            canv.Children.Add(b1);

 

 

            Label lab0 = new Label();

            lab0.Margin = new Thickness(20, 20, 0, 0);

            lab0.Width = 400;

            lab0.Height = 40;

            lab0.FontSize = 24;

            lab0.Name = "lab0";

            lab0.Content = "无XAML动态编程演示   作者:常鲲";

            canv.Children.Add(lab0);

 

            lab1 = new Label();

            lab1.Margin = new Thickness(20, 50, 0, 0);

            lab1.Width = 400;

            lab1.Height = 40;

            lab1.FontSize = 24;

            lab1.Name = "lab1";

            lab1.Content = "Location:?";

            canv.Children.Add(lab1);

//这个是Ellipse的实例对象的MouseMove事件.

            e1.MouseMove += new System.Windows.Input.MouseEventHandler(el_MouseMove);

        }

 

        void b1_Click(object sender, RoutedEventArgs e)

        {

            Point p = System.Windows.Input.Mouse.GetPosition(canv as System.Windows.IInputElement);

         //重新设置Ellipse的位置,前面讲过两种的设置方法.

            e1.Margin = new Thickness(p.X, p.Y, 0, 0);

        }

 

 

        void el_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)

        {

            Ellipse a = e.Source as Ellipse;

//获得当前鼠标的位置,这个是wpf重新封装的一个类库用System.Windows.Input.Mouse来获得鼠标的各种状态.

            Point p = System.Windows.Input.Mouse.GetPosition(canv as

System.Windows.IInputElement);

            lab1.Content = "Location:" + p.ToString();//

        }

    }

}

总体来说,这个小技术,控件查找,动态窗体,以后会用的很多.也比较简单,好理解.

下面我们来学习样式,样式其实和bs的样式表类似,只是写法不同而已.

先来看一下xaml代码.

<Window x:Class="WPF_资源和样式.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="400" Width="400" Loaded="Window_Loaded">

 

<Window.Resources >//这里我定义了静态资源

// TargetType="{x:Type Button}" 注意这句话 这是为Button设置的样式,别的控件不能使用

        <Style x:Key="GreenButtonStyle" TargetType="{x:Type Button}">

//属性样式都是以Setter开头的

            <Setter Property="Control.Width" Value="350"/>

            <Setter Property="Control.Height" Value="100"/>

            <Setter Property="Control.FontSize" Value="24"/>

//事件样式都是以EventSetter开头的  Event是指那种类型的事件 Handler是指向后台的一个方法

            <EventSetter Handler="Button_Click" Event="Click"></EventSetter>

        </Style>

        <SolidColorBrush x:Key="backgroundBrush">Yellow</SolidColorBrush>

        <SolidColorBrush x:Key="borderBrush">Red</SolidColorBrush>

    </Window.Resources>

<Grid  Background="{StaticResource borderBrush}">

//我们只需要用style来指定一下静态资源就可以了

        <Button Style="{StaticResource GreenButtonStyle}" Background="{ StaticResource backgroundBrush}">WPF 资源和样式的应用</Button>

        <Label Height="53" Margin="31,27,41,0" Name="label1" VerticalAlignment="Top" FontSize="24">WPF 中资源和样式的应用</Label>

//这个button使用的是动态资源,后面就是这个资源的代码

        <Button Content="这是通过字典项设置的样式" Margin="17,0,17,73.163" Style="{DynamicResource GreyButton}" VerticalAlignment="Bottom" Height="35.837"  />

    </Grid>

</Window>

//资源字典的代码

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="GreyButton" TargetType="Button" >

        <Setter Property="Background" Value="#00FFFF"/>

    </Style>

</ResourceDictionary>

//后台的代码

  private void Button_Click(object sender, RoutedEventArgs e)

        {

            global::System.Windows.MessageBox.Show("WPF 的 XAML 确实强大!");

        }

我们后台只有一个button的click的事件,总体来说,样式只是写法不同,并没有多少技术难度.

这章我们先认识一些比较麻烦的东西,下章开始讲解.

依赖属性,路由事件,binding,和命令,把这些讲解完毕后,wpf的基础部分就告一段落,剩下的年后再说.

转载于:https://www.cnblogs.com/chenmengmeng/archive/2012/01/17/2324431.html

你可能感兴趣的文章
ThreadPool 线程池
查看>>
AWK 文件处理计数
查看>>
我的友情链接
查看>>
AI技术说:人工智能相关概念与发展简史
查看>>
eclipse启动失败
查看>>
(已解决!)精选30道Java笔试题解答
查看>>
【Python之旅】第七篇(三):使用Redis订阅服务
查看>>
linux远程桌面链接windows
查看>>
TrendMicro:新的APT***针对亚洲和欧洲政府组织,包括中国媒体机构
查看>>
C语言中sizeof与strlen区别2
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
UIWebView加载html网页时使用缓存和清空缓存
查看>>
我的友情链接
查看>>
设计模式学习笔记(六)之策略模式(Strategy)
查看>>
python运行spark脚本程序
查看>>
我的友情链接
查看>>
通过libvirt使用ceph块设备
查看>>
优秀交互设计师成长指南
查看>>