UWP - 如何创建TokenAutoComplete控件

时间:2016-04-15 11:59:58

标签: win-universal-app uwp-xaml tokenautocomplete

我正在开发UWP(Win10 - VS2015)应用程序。我需要Windows平台中的Token TextBox。请问,如何启动和创建此控件,然后在文本框中写入文本并放置空格或只是点击该文本时,它应该转换为选定的令牌。参见图片(仅供参考)。我需要这种控制。

您也可以通过此帖TokenAutoComplete

了解相关信息

enter image description here

1 个答案:

答案 0 :(得分:1)

我发布的代码是初始代码,您可以使用..

开始构建控件

我使用了RichTextBlock和Textbox。如果你把这两个控件放在Gridview里面的WrapPanel中。你可能会得到你想要的类似控制,但我还没有尝试过。

 <RichTextBlock x:Name="tokenblock">
                <Paragraph>

                </Paragraph>

            </RichTextBlock>
            <TextBox  TextChanged="TextBox_TextChanged"/>

背后的代码是这样的

 private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string text = (sender as TextBox).Text;
            if (text.Contains(';'))
            {
                Paragraph para;
                text = text.Substring(0, text.LastIndexOf(';'));
                if (tokenblock.Blocks.Count > 0)
                  para  = tokenblock.Blocks[0] as Paragraph;
                else
                 para = new Paragraph();
                InlineUIContainer inline = new InlineUIContainer();
                Border br = new Border();
                br.Background = new SolidColorBrush(Colors.Gray);
                br.CornerRadius = new CornerRadius(10);
                TextBlock tb = new TextBlock();
                br.MinWidth = 70;
                br.MaxWidth = 150;
                tb.Text = text;
                tb.TextWrapping = TextWrapping.Wrap;
                tb.Margin =new Thickness(10, 10, 10, 10);
                br.Child = tb;
                inline.Child = br;
                para.Inlines.Add(inline);
                (sender as TextBox).Text = "";
            }

//下面的代码我还没试过

   <GridView x:Name="myGridView" IsItemClickEnabled="True">
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <ItemsWrapGrid Orientation="Horizontal" MaximumRowsOrColumns="5"/>
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
//here  you have to put RichTextBlock and textbox as two gridview items