win-universal-appuwp-xamltokenautocomplete

UWP - How to create a TokenAutoComplete Control


I am developing UWP (Win10 - VS2015) App. I need a Token TextBox in Windows Platform. Any idea please, how to start and create this control, then when writing text inside the Textbox and put space or just tap that text, it should convert into selected Token. See the pic (its just for idea). I need such type of control.

You can also get idea from this Post TokenAutoComplete

enter image description here


Solution

  • The code which I'm posting is initial code you can start builting control with..

    I used RichTextBlock and Textbox. If you put these two controls in WrapPanel inside the Gridview. You might get similar control which you wanted but I haven't tried it.

     <RichTextBlock x:Name="tokenblock">
                    <Paragraph>
    
                    </Paragraph>
    
                </RichTextBlock>
                <TextBox  TextChanged="TextBox_TextChanged"/>
    

    Code behind is like this

     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 = "";
                }
    

    //below codes I haven't tried

       <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