WPF 读书笔记之五 按钮和其他控件(Applications=Code+Markup)

已经学习了一些基本的概念,今天主要学习Button以及其他控件。

1. Button

Button继承自ButtonBase,ButtonBase继承自ContentControl. ContentControl继承自Content.可以得知Button和Window一样具有相同的属性Content.因为Window也是继承自ContentControl,Content。 那么可以设置给window的content内容也可以用于Button。以下简要介绍Button的属性

Content:设置按钮的内容

Margin:让按钮周围有一定的边框。 btn.Margin=new Thickness(96); 因为我们在WPF里面使用的单位都是设备无关单位(1/96英寸),所以这个设置代表按钮的4边都具有1英寸的空间。注意Margin和StrokeThickness,Padding是不一样的。StrokeThickness是边框。Padding是按钮内侧和按钮内容时间的空间设置。大家可以试验一下

Untitled

 

HorizontalContentAlignment: 按钮content的水平位置:Center, Left, Right Stretch (注意这个和HorizontalAlignment是不一样的,名称中间多了Content)

VerticalContentAlignment: 按钮content的垂直位置:Center, Bottom,, Top, Stretch (注意这个和VerticalAlignment是不一样的,名称中间多了Content)

HorizontalAlignment: 设置按钮的水平位置

VerticalAlignment: 设置按钮的垂直位置

 

Command:可以将Button的command设置为ApplicationCommands, ComponentCommands, MediaCommands, NavigationCommands或EditingCommands.  前面四个属于System.Windows.Input空间, 后面的属于System.Windows.Documents命名空间。这些类的静态属性是属于RoutedUICommand. 如你可以设置一个按钮进行Paste命令:

    btn.Command=ApplicationCommands.Paste;

 

CommandBinding: 将命令绑定。如:

    CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste,PasteOnExecute,PasteCanExecute));

 

2. ToggleButton

ToggleButton不是抽象类,它继承自ButtonBase, 它具有Checked, unChecked状态,CheckBox, RadioButton都是继承自ToggleButton. 以下不是部分属性

IsThreeState: 如果设置为true,ToggleButton的状态就可以设置为Checked, Unchecked, Null. 就是说IsChecked有3个可能的返回值:true,false,null。

 

3. Label. 很简单,就不多作介绍了

 

4. TextBox

TextBox继承自TextBoxBase。以下是部分属性:

AcceptsReturn:支持回车键

Focus:获得输入焦点

CaretIndex:设置光标的位置,如

    if(args.Key==Key.F5) //用户按F5,就会在TextBox里面光标的位置插入当前的时间字符串

    {

        txtBox.SelectedText=DateTime.Now.ToString();

        txtBox.CaretIndex=txtBox.SelectionStart+txtBox.SelectionLength;

    }

 

5. RichTextBox

RichTextBox支持RTF (Rich Text Format). 来帖上一段代码:[来自书中]

using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace Petzold.EditSomeRichText
{
    public class EditSomeRichText : Window
    {
        RichTextBox txtbox;
        string strFilter =
            "Document Files(*.xaml)|*.xaml|All files (*.*)|*.*";

        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new EditSomeRichText());
        }
        public EditSomeRichText()
        {
            Title = "Edit Some Rich Text";

            txtbox = new RichTextBox();
            txtbox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            Content = txtbox;

            txtbox.Focus();
        }
        protected override void OnPreviewTextInput(TextCompositionEventArgs args)
        {
            if (args.ControlText.Length > 0 && args.ControlText[0] == '\x0F')
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.CheckFileExists = true;
                dlg.Filter = strFilter;

                if ((bool)dlg.ShowDialog(this))
                {
                    FlowDocument flow = txtbox.Document;
                    TextRange range = new TextRange(flow.ContentStart,
                                                    flow.ContentEnd);
                    Stream strm = null;

                    try
                    {
                        strm = new FileStream(dlg.FileName, FileMode.Open);
                        range.Load(strm, DataFormats.Xaml);  //TextRange具有Save和Load方法, DataFormats可以指定为XAML,RTF,XAMLPackage…
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message, Title);
                    }
                    finally
                    {
                        if (strm != null)
                            strm.Close();
                    }
                }

                args.Handled = true;
            }
            if (args.ControlText.Length > 0 && args.ControlText[0] == '\x13')
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = strFilter;

                if ((bool)dlg.ShowDialog(this))
                {
                    FlowDocument flow = txtbox.Document;  //RichTextBox的Document属性返回FlowDocument
                    TextRange range = new TextRange(flow.ContentStart,
                                                    flow.ContentEnd);
                    Stream strm = null;

                    try
                    {
                        strm = new FileStream(dlg.FileName, FileMode.Create);
                        range.Save(strm, DataFormats.Xaml); //TextRange具有Save和Load方法, DataFormats可以指定为XAML,RTF,XAMLPackage…
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message, Title);
                    }
                    finally
                    {
                        if (strm != null)
                            strm.Close();
                    }
                }
                args.Handled = true;
            }
            base.OnPreviewTextInput(args);
        }
    }
}

 

好了,本篇就写到这里,了解基本控件的某些属性对以后实践有很大帮助。

转载于:https://www.cnblogs.com/tobemvp/archive/2010/08/11/1797367.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值