WP7学习笔记(一) 常用控件属性和事件介绍

1  Button控件

1.1创建button控件的方法

a:从工具箱直接拖放控件

bXAML方法 在.xaml文件中写入下面这个代码:

   <Button Content="拖放控件" Height="72" HorizontalAlignment="Left" Margin="195,43,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />

cC#方法 从后台创建

       Button btn = new Button();

       btn.Content = "新闻30分";

       this.ContentPanel.Children.Add(btn);   //ContentPanel为Grid的名称

1.2给控件添加事件

a:在xaml中添加控件事件

  <Button Content="拖放控件" Height="72" HorizontalAlignment="Left" Margin="195,43,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />

   在后台中事件中写下code

  private void button1_Click(object sender, RoutedEventArgs e)

        {

            MessageBox.Show("hello boy ,this is drag controller");

           

        }

b:在c#中添加控件事件

 button1.Click += (seder,e) => {

                button2.Content = "i am get gun";

            };

1.3 内建控件的使用

使用TextBoxRectangleImage控件作为Button控件的Content属性值

            <Button Width="200" Height="200" Margin="193,240,0,0"  HorizontalAlignment="Left" VerticalAlignment="Top">

                <Button.Content>

                    <TextBox Text="这是内建控件" FontSize="18"></TextBox>

                    <!--<Rectangle Width="120" Height="80" Fill="Red"></Rectangle>-->

                    <!--<Image Source="background.png"></Image>-->

                </Button.Content>

            </Button>

2  CheckBox控件

CheckBox控件常用的属性有IsCheckedIsThreeState

IsChecked:获取或设置CheckBox控件是否为选中状态

IsThreeState:获取或设置CheckBox支持两种状态(True/False) 还是三种状态(选中/不选中/不确定)

 

3  HyperLinkButton控件

Content :HyperlinkButton显示的内容,支持任意字符

NavigateUri:页面Uri即链接地址 

IsEnabled:设置为True时为激活状态,设置为false为无效状态

<HyperlinkButton Content="MainPage页面" IsEnabled="True" Margin="320,30,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" NavigateUri="/MainPage.xaml" ></HyperlinkButton>

 

4 Image控件

Source属性 图片地址的源 支持本地图片和网络图片

Stretch属性 Image控件的呈现状态  设置内容如下:

            None:不对图片进行拉伸,以便填充规定的尺寸

            Fill:不按比例拉伸,完全填充规定的尺寸区域

            Uniform: 按比例进行拉伸,直到有一边满足规定的尺寸区域为止,其余部分不被填充,按最小尺寸填充

            UniformTOFill时 按比例进行拉伸,直到完全填充规定的尺寸区域为止,超出该区域的将会被剪裁,按最大尺寸填充。

 

控件的遍历:

  //ContentPanel为Grid的Name

  foreach (var item in this.ContentPanel.Children)

            {

                System.Windows.Controls.CheckBox chk;

                if (item.GetType() == typeof(System.Windows.Controls.CheckBox))

                {

                    chk = item as System.Windows.Controls.CheckBox;

                    chk.IsEnabled = true;

                    chk.IsChecked = false;

                }

            }

6 ListBox控件

ListBox(列表)控件是一个以列形式列出数据的控件。 ListBox控件包含多个ListItem(数据项)。

Item不仅可以是文本数据,还可以是其他控件元素。

代码如下:

<ListBox Height="143" HorizontalAlignment="Left" Margin="70,59,0,0" Name="listbox1" VerticalAlignment="Top" Width="220" SelectionChanged="listbox1_SelectionChanged">

                <ListBoxItem Content="第一项"></ListBoxItem>

                <ListBoxItem Content="第二项"></ListBoxItem>

                <ListBoxItem Content="第三项"></ListBoxItem>

                <ListBoxItem Content="第④项"></ListBoxItem>

                <ListBoxItem Content="第五项"></ListBoxItem>

                <ListBoxItem Content="第⑥项"></ListBoxItem>

</ListBox>

后台事件listbox1_SelectionChanged代码如下

  TextBlock1.Text = (listbox1.SelectedItem as ListBoxItem).Content.ToString();

 

7 PasswordBox控件

密码框 常用属性有

Password:设置passwordBox的初始化字符

IsEnabled: 当设置为true时,此控件才可以用

MaxLength:最多能接收用户输入的字符数

PasswordChar:设置或更改代替字符

示例代码如下:

            <PasswordBox   Margin="10,100,0,0" MaxLength="10"  Width="240"

                            PasswordChar="#" Password="gwt" Height="90"  >

            </PasswordBox>

 

8 ProgressBar控件

常用属性有 Value、IsEnabled、IsIndeterminate、Maximum、Minimum

Value:该属性决定ProgressBar的载入进度。

IsIndeterminate:更改Isindeterminate属性值,可以更改ProgressBar控件的外观。当设置为 True时,页面将加载一个重复循环的动画。当设置为false时,进度条会根据value值进行显示。

 

9 RadioButton控件

Content:获取或设置RadioButton控件的显示内容

GroupName:获取或设置RadioButton所属的组名

IsThreeSate:确定RadioButton控件支持两种或三种状态

<RadioButton Content="不男不女" Height="72" HorizontalAlignment="Left" Margin="60,400,0,0" VerticalAlignment="Top"  IsThreeState="True"  Name="radio5">

</RadioButton>

 

10 Slider控件

Value获取或设置Slider控件的值

Minimum:Slider控件的value属性值能接受的最小值 默认为0

Maximun:Slider控件的Value属性值能接受的最大值,默认为10;

Orientation:Slider布局方向 水平还是垂直 

IsDirectionReversed:Slider控制方向相反,滑动越多值越小

前台代码:

            <Slider Height="110" HorizontalAlignment="Left" Margin="19,439,0,0"

                    Name="Slider1" VerticalAlignment="Top" Width="400"

                    IsDirectionReversed="False" Orientation="Horizontal" Maximum="100">

                

            </Slider>

后台事件:

Slider1.ValueChanged+=new RoutedPropertyChangedEventHandler<double>(slider_ValueChang);

void slider_ValueChang(object sender, RoutedPropertyChangedEventArgs<double> e) {}

 

11 TextBlock控件

TexbBlock文本的格式化

<Run>标记元素: 此标记尅设置不同文本的属性和样式等

<LineBreak/> 类似于web中的<br/>

TextBlcok控件中的text属性完全支持笔刷功能。 可以制作出入渐变文字和图片镂空文字等

代码如下:

 <!--利用TextBlock笔刷属性可以将文本设置成渐变的效果-->

            <TextBlock  Height="79" FontSize="34" HorizontalAlignment="Left" Margin="10,10,0,0Name="textBlock2" Text="科技新闻" VerticalAlignment="Top" Width="438">

                <TextBlock.Foreground>

                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                        <GradientStop

                             Color="#FF00FF76"></GradientStop>

                        <GradientStop

                             Color="#ffe0e200" Offset="1"></GradientStop>

                    </LinearGradientBrush>

                </TextBlock.Foreground>

            </TextBlock>

            <!--利用各种标记元素可以格式化文本-->

            <TextBlock  Height="323" HorizontalAlignment="Left" Margin="6,78,0,0" Name="textBlock1"   VerticalAlignment="Top" Width="441" TextWrapping="Wrap">

                <Run FontSize="26" Foreground="Red">

                    2010年12月27日

                </Run>:<LineBreak/>

                据国外媒体报道,微软公司 鲍尔默北京时间2月5日在全球移动通信的阿站上发布了公司最新一代手机操作

                系统,windows phone7.微软称,公司将通过最新一代手机操作系统windows phone7,将旗下Xbox LIVE

                游戏、Zune音乐与独特的视频体验整合至手机中。<LineBreak/>

                2010年10月11日晚上9点30分,微软公司正式发布了智能手机操作系统Windows Phone7,并同时宣布了首批

                采用Windows Phone7的智能手机只有9款,最早将于10月21日上市。

            </TextBlock>

            <!--利用图片作为笔刷的源-->

            <TextBlock Height="69" HorizontalAlignment="Left" FontSize="50" Margin="6,407,0,0"    Text="Windows Phone" VerticalAlignment="Top" Width="441">

                <TextBlock.Foreground>

                    <ImageBrush Stretch="Fill" ImageSource="Background.png"/>

                </TextBlock.Foreground>

            </TextBlock>

 

12 TextBox控件

常用属性有:

AcceptsReturn:设置为True时,接受多行文本输入;设置为False时,只能输入一行文本。

TextWrapping:设置或获取TextBox控件的自动换行状态 设置为wrap 自动换行,为NoWrap时,不自动换行。

HorizontalScrollBarVisibility:水平方向滚动条,Disabled 滚动条失去激活状态;Auto 根据需要自动显示与隐藏;当值为hidden时,始终隐藏,当值为Visible,始终可见

VerticalScrollBarVisibility:垂直方向滚动条 ;同上。

MaxLength:最多能接受的字符数量,超过后无法粘贴进入,但可以继续输入;

代码如下:

     <TextBox Height="364" HorizontalAlignment="Left" Margin="12,24,0,0" Name="textBox1" Text=""VerticalAlignment="Top"Width="421"AcceptsReturn="True"  HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible"TextChanged="textBox1_TextChanged"  TextWrapping="Wrap"  > </TextBox>

 

13 WebBrowser控件

常用属性:

Width:设置控件的宽度

Height:设置控件的高度

Source:WebBrowser控件要显示的网页源;可以使本地或者远程的一个网页地址

IsScriptEnabled:设置为True时,启动脚本功能。

事件:

Loaded:载入时触发

LoadCompleted:载入完成后触发

Navigating:导航进行时触发

Navigated:导航完成后触发

前台代码:

<phone:WebBrowser Height="476" Margin="12,84,0,0" 

                              Name="webBrowser1" VerticalAlignment="Top"

                              Width="438" Source="http://www.baidu.com"

                              UseLayoutRounding="True"></phone:WebBrowser>

         

后台代码:

public MainPage()

        {

            InitializeComponent();

            button1.Click += new RoutedEventHandler(button1_Click);

            webBrowser1.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(webBrowser1_LoadCompleted);

        }

Void webBrowser1_LoadCompleted(object sender,

System.Windows.Navigation.NavigationEventArgs e)

        {

            textBlock1.Text = "网页载入完成";

        }

原文地址:https://www.cnblogs.com/flowwind/p/2794736.html