WPF Label to TextBox

后端 未结 5 1778
长情又很酷
长情又很酷 2021-01-18 11:39

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 p

5条回答
  •  难免孤独
    2021-01-18 12:07

    Here is a control that does it:

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    public class KeyValueControl : Control
    {
        public static readonly DependencyProperty KeyProperty = DependencyProperty.Register(
            "Key",
            typeof(string),
            typeof(KeyValueControl),
            new PropertyMetadata(default(string)));
    
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
            "Value",
            typeof(object),
            typeof(KeyValueControl),
            new FrameworkPropertyMetadata
            {
                DefaultValue = null,
                BindsTwoWayByDefault = true,
                DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
            });
    
        static KeyValueControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(KeyValueControl), new FrameworkPropertyMetadata(typeof(KeyValueControl)));
        }
    
        public string Key
        {
            get
            {
                return (string)GetValue(KeyProperty);
            }
            set
            {
                SetValue(KeyProperty, value);
            }
        }
    
        public object Value
        {
            get
            {
                return GetValue(ValueProperty);
            }
            set
            {
                SetValue(ValueProperty, value);
            }
        }
    }
    

    Style:

    
    

    Usage (create a property grid):

    
        
        
        
        
        
    
    

提交回复
热议问题