WPF标签到文本框 [英] WPF Label to TextBox

查看:187
本文介绍了WPF标签到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最好的做法,以显示文本标签(如姓名)与WPF的文本框? 我希望有一个标签名称文本框和许多类似的标签/文本框的上方。 我应该把对标签/文本框到垂直的StackPanel?

what is the best practice to show text label (e.g. "Name") with the TextBox in WPF? I want a label "Name" above the TextBox and many similar Labels/TextBoxes. Should I put the pairs Label/TextBox into the vertical StackPanel?

有没有简单的解决方案?

Is there a simpler solution?

推荐答案

这真的取决于你想要做的,将来这些控件是什么。如果你想重新使用这种控制多次(也许动态创建的话),这将是最好的创建用户控件和编程。然后,您可以轻松地重用一个非常简单的方式(如将在StackPanel中)。

It really depends on what you want to do with these controls in the future. If you want to reuse this kind of control multiple times (and maybe create it on the fly), it would be the best to create UserControl and program it. You can then easily reuse it in a very simple manner (like putting in on StackPanel).

$ C $下LabelTextBox.xaml

Code for LabelTextBox.xaml

<UserControl x:Class="YourProject.LabelTextBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="49" d:DesignWidth="314" MinHeight="49" MaxHeight="49">
    <Grid>
        <Label Content="Label" Height="28" HorizontalAlignment="Left" Name="BaseLabel" VerticalAlignment="Top" />
        <TextBox Height="23" Margin="0,26,0,0" Name="BaseTextBox" VerticalAlignment="Top" />
    </Grid>
</UserControl>

$ C $下LabelTextBox.xaml.cs

Code for LabelTextBox.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace YourProject
{
    /// <summary>
    /// Interaction logic for LabelTextBox.xaml
    /// </summary>
    public partial class LabelTextBox : UserControl
    {
        public LabelTextBox()
        {
            InitializeComponent();
        }



        string LocalLabel = "";
        string LocalTextBox = "";

        public string Label
        {
            get { return LocalLabel; }
            set
            {
                LocalLabel = value;
                BaseLabel.Content = value;
            }
        }

        public string TextBox
        {
            get { return LocalTextBox; }
            set
            {
                LocalTextBox = value;
                BaseTextBox.Text = value;
            }
        }
    }
}

您可以更改标签文本和文本框的内容与新的控制(隐藏属性的设计师其他部分的标签和文本框属性,还可以计划的更多功能的用户控件。

You can change Label text and TextBox content with Label and TextBox property of new control (hidden in "Other" part of Properties in designer. You can also program additional functions for the UserControl.

如果不需要重新使用这些控制这么多,其他的解决方案就足够了。

If you don't need to reuse these controls so much, other solutions will suffice.

这篇关于WPF标签到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆