如下图,这个效果怎么做,点击Add,ListBox中会有一行等待添加,填完鼠标离开自动添加完成,这个效果使用C#怎么来做?

解决方案 »

  1.   

    用Textinput控件,点击一下就显示这个控件在你想要的位置,然后回车就取消这个控件显示变为lable 控件或者其他你想要的控件。
      

  2.   

    大佬有没有什么例子可以参考一下吗?我是搞BS的,不怎么会写CS端自定义控件呢。
      

  3.   

    你点击add的时候在你设定的位置生成文本控件试试
      

  4.   

    用一个面板控件 往里边添加一个TextBox控件。
      

  5.   

    MainWindow.xaml:
    <Window x:Class="ListBoxTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/up-compatibility/2006"
            xmlns:local="clr-namespace:ListBoxTest"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525" Background="Gray">
        <Window.Resources>
            <DataTemplate x:Key="ListBoxTemplate">
                <TextBox  HorizontalAlignment="Left" VerticalAlignment="Top" Width="97" Margin="0,5,0,5" FontFamily="Arial" Foreground="Black" FontSize="20" Background="White"/>
            </DataTemplate>
            <Style TargetType="ListBoxItem" x:Key="ListBoxBack">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF38474E"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF38474E"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FF38474E"/>
                </Style.Resources>
            </Style>
        </Window.Resources>
        <Grid>
            <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="100" BorderThickness="0" Margin="125,80,0,0" VerticalAlignment="Top" Width="100"  ItemContainerStyle="{StaticResource  ListBoxBack}" ItemsSource="{Binding TextBoxList}" ItemTemplate="{StaticResource  ListBoxTemplate}" SelectedIndex="{Binding SelectIndex}"/>
            <Button x:Name="AddBtn" Content="Add" HorizontalAlignment="Left" Margin="283,89,0,0" VerticalAlignment="Top" Width="75" Command="{Binding AddCommand}"/>
            <Button x:Name="DeleteBtn" Content="Delete" HorizontalAlignment="Left" Margin="283,149,0,0" VerticalAlignment="Top" Width="75" Command="{Binding DeleteCommand}"/>
        </Grid>
    </Window>MainWindow.xaml.cs:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    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 ListBoxTest
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();            var vm = new MainWindowViewModel();
                this.DataContext = vm;
            }
        }
    }MainWindowViewModel.cs:
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Practices.Prism.ViewModel;
    using Microsoft.Practices.Prism.Commands;
    using System.Windows.Controls;namespace ListBoxTest
    {
        public class MainWindowViewModel : NotificationObject
        {
            private ObservableCollection<TextBox> _textBoxList = new ObservableCollection<TextBox>();
            public ObservableCollection<TextBox> TextBoxList
            {
                get
                {
                    return _textBoxList;
                }
                set
                {
                    if (_textBoxList != value)
                    {
                        _textBoxList = value;
                        this.RaisePropertyChanged(() => this.TextBoxList);
                    }
                }
            }        private int _selectIndex = 0;
            public int SelectIndex
            {
                get
                {
                    return _selectIndex;
                }
                set
                {
                    _selectIndex = value;
                    this.RaisePropertyChanged(() => this.SelectIndex);
                }
            }        protected DelegateCommand<object> _addCommand = null;
            public DelegateCommand<object> AddCommand
            {
                get
                {
                    if (_addCommand == null)
                    {
                        _addCommand = new DelegateCommand<object>(OnAddCommand, CanAddCommand);
                    }
                    return _addCommand;
                }
            }        private bool CanAddCommand(object para)
            {
                return true;
            }        private void OnAddCommand(object para)
            {
                Add();
            }        private void Add()
            {
                if (TextBoxList != null)
                {
                    TextBoxList.Add(new TextBox { Text = "None"});
                }
            }        protected DelegateCommand<object> _deleteCommand = null;
            public DelegateCommand<object> DeleteCommand
            {
                get
                {
                    if (_deleteCommand == null)
                    {
                        _deleteCommand = new DelegateCommand<object>(OnDeleteCommand, CanDeleteCommand);
                    }
                    return _deleteCommand;
                }
            }        private bool CanDeleteCommand(object para)
            {
                return true;
            }        private void OnDeleteCommand(object para)
            {
                Delete();
            }        private void Delete()
            {
                if (SelectIndex >= 0 && SelectIndex <= TextBoxList.Count)
                {
                    TextBoxList.RemoveAt(SelectIndex);
                }
            }
        }菜鸟瞎写的,里面还有点问题,你看看能不能帮到你
      

  6.   

    不知道winform的flowlayoutpanel行吗?控件都往上添加或移除,好像也能实现你的功能 public Form2()
            {
                InitializeComponent();
                listTextBoxs = new List<TextBox>();
            }        List<TextBox> listTextBoxs;
            private void Form2_Load(object sender, EventArgs e)
            {
                this.flowLayoutPanel1.Dock = DockStyle.Fill;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                TextBox t1 = new TextBox();            listTextBoxs.Add(t1);            ShowTextBoxs();
            }        private void button2_Click(object sender, EventArgs e)
            {
                if (listTextBoxs.Count>0)
                {
                    listTextBoxs.RemoveAt(listTextBoxs.Count - 1);
                    ShowTextBoxs();
                }
            }        private void ShowTextBoxs()
            {
                this.flowLayoutPanel1.Controls.Clear();
                foreach (TextBox t in listTextBoxs)
                {
                    this.flowLayoutPanel1.Controls.Add(t);
                }
            }