新时尚Windows8开发(18):制作类似单行簿的输入控件

来源:互联网 发布:js判断div加载完成 编辑:程序博客网 时间:2024/04/29 07:01

单行簿,在上小学,初中的时候,相信我们都用过,那我们在让用户输入内容的,如文本框,也能做出这种格式吗?

对了,新浪微博Win8客户端,在发表新微博的时候,输入框也是这样的效果,看看下面的图。

 

新浪微博这个输入控件,是先把TextBox的背景设置为{x:Null},然后在TextBox下面放一个Image控件,再引用一张图片来实现的。

而我的做法也差不多,只是在TextBox下面放的不是图片,而是一个Grid元素,Grid分为几行,而每一行放一个高度为1的矩形,看起来就像一根直线,同样,也做出了这种效果。请看下图。

 

怎么样?还过得去吧,虽然不是什么高技术含量,但还是初步实现了类似单行簿的输入界面。

 

方法倒不复杂,首先,用一个用户控件,把这些东西排版好。

<UserControl    x:Class="App1.ucxaml"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:App1"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d"    Width="450"    Height="270">        <UserControl.Resources>        <Style x:Key="lineStyle" TargetType="Rectangle">            <Setter Property="VerticalAlignment" Value="Bottom"/>            <Setter Property="Fill" Value="Red"/>            <Setter Property="HorizontalAlignment" Value="Stretch"/>            <Setter Property="Height" Value="1"/>            <Setter Property="Opacity" Value="0.6"/>        </Style>        <Style x:Key="txtStyle" TargetType="TextBox">            <Setter Property="Background" Value="{x:Null}"/>            <Setter Property="Foreground" Value="Black"/>            <Setter Property="FontSize" Value="28"/>            <Setter Property="TextWrapping" Value="Wrap"/>        </Style>    </UserControl.Resources>        <Grid Background="LightGray">        <Grid Canvas.ZIndex="0">             <Grid.RowDefinitions>                <RowDefinition Height="40"/>                <RowDefinition Height="38"/>                <RowDefinition Height="38"/>                <RowDefinition Height="38"/>                <RowDefinition Height="38"/>            </Grid.RowDefinitions>            <Rectangle Grid.Row="0" Style="{StaticResource lineStyle}"/>            <Rectangle Grid.Row="1" Style="{StaticResource lineStyle}"/>            <Rectangle Grid.Row="2" Style="{StaticResource lineStyle}"/>            <Rectangle Grid.Row="3" Style="{StaticResource lineStyle}"/>            <Rectangle Grid.Row="4" Style="{StaticResource lineStyle}"/>        </Grid>        <TextBox Name="txt" Canvas.ZIndex="1" Style="{StaticResource txtStyle}" MaxLength="120"/>    </Grid></UserControl>


对于行的高度如何与TextBox中的文本配合,我们就不断地调,直到看起来差不多就行了,就是协调TextBox中的字体大小和Grid的行高。

 

然后,我们在其他页面中引用这个用户控件就行了。

<Page    x:Class="App1.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:App1"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d">    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">        <local:ucxaml />    </Grid></Page>


这是一个笨方法,让各位见笑了,呵呵,如果你觉得有用,就研究一下,如果你想到更强大的方法,你就不用看了。哈!

原创粉丝点击