WPF C#ComboBox有多行并接受退货?

时间:2014-09-30 20:21:59

标签: c# wpf combobox

在WPF C#中有一个简单的方法来做多线组合框?这是什么意思?我的意思是一个支持多行和回车的文本框;它包装的地方...它需要有一个滚动条,这样如果框中的文字高于组合框的高度,用户可以向下滚动。

此外,由于它是一个组合框,它需要有一个下拉按钮,以便用户可以在文本组之间快速切换。我已经尝试使用谷歌搜索,但我找不到任何人在谈论这样的组合框。

1 个答案:

答案 0 :(得分:4)

编辑完整的工作解决方案:

XAML:

    <Window x:Class="delete.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Height="30" Width="300" ItemsSource="{Binding items}" SelectedItem="{Binding item}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBox AcceptsReturn="True" TextWrapping="Wrap" Width="250" Height="30" Text="{Binding Name}" VerticalScrollBarVisibility="Auto"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>

代码隐藏:

    using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
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 delete
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            Setup();
            this.DataContext = this;
        }

        ObservableCollection<Thang> _items;
        public ObservableCollection<Thang> items
        {
            get { return _items; }

            set
            {
                _items = value;
                OnPropertyChanged("items");
            }
        }

        private Thang _item;
        public Thang item
        {
            get { return _item; }
            set
            {
                _item = value;
                OnPropertyChanged("item");
            }
        }

        public void Setup()
        {
            items = new ObservableCollection<Thang>();
            items.Add(new Thang("1", "One"));
            items.Add(new Thang("2", "Two"));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

    public class Thang
    {
        public Thang(string id, string name)
        {
            Name = name;
            ID = id;
        }

        public string Name { get; set; }
        public string ID { get; set; }
    }
}