WPF文本框中的列表框-芯片

WPF文本框中的列表框-芯片,wpf,xaml,listbox,wpf-controls,custom-controls,Wpf,Xaml,Listbox,Wpf Controls,Custom Controls,我需要在文本框中添加列表框,即芯片 参考屏幕截图:(期望值) 只考虑视图模型: public class Person { private ObservableCollection<string> _personList = new ObservableCollection<string>(); public ObservableCollection<string> PersonList { get

我需要在文本框中添加列表框,即芯片

参考屏幕截图:(期望值

只考虑视图模型:

public class Person
{
    private ObservableCollection<string> _personList = new ObservableCollection<string>();

    public ObservableCollection<string> PersonList
        {
            get { return _personList; }
            set
            {
                _personList = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("PersonList"));
            }
        }

    private string _personStr = String.Empty;

    public string PersonStr
        {
            get { return _personStr; }
            set
            {
                _personStr = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("PersonStr"));
            }
        }

    public Person()
        {
            PersonList.Add("IR-Punch");
            PersonList.Add("Stack-Overflow");
        }

    public ICommand BTextCommand
        {
            get
            {
                return new DelegateCommand(AppendString);
            }
        }

    public void AppendString()
        {
            PersonList.Add(PersonStr);
        }
}
公共类人物
{
私有ObservableCollection_personList=新ObservableCollection();
公共观察收集人员
{
获取{return\u personList;}
设置
{
_个人主义=价值;
如果(PropertyChanged!=null)PropertyChanged(这是新的PropertyChangedEventArgs(“PersonList”);
}
}
私有字符串_personStr=string.Empty;
公共字符串PersonStr
{
获取{return\u personStr;}
设置
{
_personStr=价值;
如果(PropertyChanged!=null)PropertyChanged(这是新的PropertyChangedEventArgs(“PersonStr”);
}
}
公众人士()
{
个人列表。添加(“IR打孔”);
添加(“堆栈溢出”);
}
公共ICommand BTextCommand
{
得到
{
返回新的DelegateCommand(AppendString);
}
}
public void AppendString()
{
PersonList.Add(PersonStr);
}
}
工作XAML源代码:

<ItemsControl ItemsSource="{Binding PersonList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding }" TextWrapping="Wrap"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

<TextBox Text="{Binding PersonStr}" Width="160" VerticalAlignment="Center" />

<Button Command="{Binding BTextCommand}" Content="Add" />


请帮助我如何在
文本框中添加
列表框
。我照顾赛特。我期待核心想法。

不要尝试在TextBox中嵌入其他控件,而是将ItemsControl和TextBox嵌入堆栈面板中,如下所示:

<Border BorderThickness="1">
    <ScrollViewer VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto">
        <StackPanel Orientation="Horizontal">
            <ItemsControl ItemsSource="{Binding PersonList}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <TextBox MinWidth="100" BorderBrush="Transparent" BorderThickness="0" Text="{Binding PersonStr}" />
        </StackPanel>
    </ScrollViewer>
</Border>

您可能希望将外部边框的样式设置为文本框(基本上只是找到正确的边框笔刷),但根据要求,我将样式设置留给您


请注意,我实际上并没有尝试过这个解决方案,只是把它写成了一种应该有效的方法。如果有任何问题,请告诉我。

不要尝试在TextBox中嵌入其他控件,而是将ItemsControl和TextBox嵌入堆栈面板,如下所示:

<Border BorderThickness="1">
    <ScrollViewer VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto">
        <StackPanel Orientation="Horizontal">
            <ItemsControl ItemsSource="{Binding PersonList}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <TextBox MinWidth="100" BorderBrush="Transparent" BorderThickness="0" Text="{Binding PersonStr}" />
        </StackPanel>
    </ScrollViewer>
</Border>

您可能希望将外部边框的样式设置为文本框(基本上只是找到正确的边框笔刷),但根据要求,我将样式设置留给您


请注意,我实际上并没有尝试过这个解决方案,只是把它写成了一种应该有效的方法。如果您有任何问题,请告诉我。

您可以从代码隐藏中尝试,并通过编程在文本框中添加列表框。这就是我要问的想法……这里有点不清楚您要的是什么。你是说你想开始在一个文本框中输入并在下拉列表中显示一个选项列表吗?你可以从代码隐藏中尝试,并通过编程在文本框中添加列表框。这就是我想问的想法,我想知道如何编写它……这里有点不清楚你想要什么。你是说你想开始在文本框中输入,并在下拉列表中显示选项列表吗?谢谢。我试试这个。谢谢。我试试这个。