private TokenItem CreateTokenItem( Token token )
    {
      object item = token.Item;

      var tokenItem = new TokenItem()
      {
        TokenKey = token.TokenKey,
        Content = item,
        ContentTemplate = TokenTemplate
      };

      if( TokenTemplate == null )
      {
        //if no template was supplied let's try to get a value from the object using the DisplayMemberPath
        if( !String.IsNullOrEmpty( DisplayMemberPath ) )
        {
          var property = item.GetType().GetProperty( DisplayMemberPath );
          if( property != null )
          {
            var value = property.GetValue( item, null );
            if( value != null )
              tokenItem.Content = value;
          }
        }
      }

      return tokenItem;
    }
 private InlineUIContainer CreateTokenContainer( Token token )
 {
   return new InlineUIContainer( CreateTokenItem( token ) )
   {
     BaselineAlignment = BaselineAlignment.Center
   };
 }
    private Token ResolveTokenBySearchMemberPath( string searchText )
    {
      //create a new token and default the settings to the search text
      var token = new Token( TokenDelimiter )
      {
        TokenKey = searchText,
        Item = searchText
      };

      if( ItemsSource != null )
      {
        foreach( object item in ItemsSource )
        {
          var searchProperty = item.GetType().GetProperty( SearchMemberPath );
          if( searchProperty != null )
          {
            var searchValue = searchProperty.GetValue( item, null );
            if( searchText.Equals( searchValue.ToString(), StringComparison.InvariantCultureIgnoreCase ) )
            {
              var valueProperty = item.GetType().GetProperty( ValueMemberPath );
              if( valueProperty != null )
                token.TokenKey = valueProperty.GetValue( item, null ).ToString();

              token.Item = item;
              break;
            }
          }
        }
      }

      return token;
    }
    private void ReplaceTextWithToken( string inputText, Token token )
    {
      _surpressTextChangedEvent = true;
        var ok = !string.IsNullOrWhiteSpace(inputText.Trim().Trim(TokenDelimiter.ToCharArray()));
      var para = _rtb.CaretPosition.Paragraph;

      var matchedRun = para.Inlines.FirstOrDefault( inline =>
      {
        var run = inline as Run;
        return ( run != null && run.Text.EndsWith( inputText ) );
      } ) as Run;

      if( matchedRun != null ) // Found a Run that matched the inputText
      {
        var tokenContainer = CreateTokenContainer( token );
        para.Inlines.InsertBefore(matchedRun, tokenContainer);

        // Remove only if the Text in the Run is the same as inputText, else split up
        if( matchedRun.Text == inputText )
        {
          para.Inlines.Remove( matchedRun );
        }
        else // Split up
        {
          var index = matchedRun.Text.IndexOf( inputText ) + inputText.Length;
          var tailEnd = new Run( matchedRun.Text.Substring( index ) );
          para.Inlines.InsertAfter( matchedRun, tailEnd );
          para.Inlines.Remove( matchedRun );
        }

        //now append the Text with the token key
        if (!ok) para.Inlines.Remove(tokenContainer);
      }
      _surpressTextChangedEvent = false;
    }
 public void InitializeTokensFromText(string text)
 {
   if( !String.IsNullOrEmpty( text ) )
   {
     string[] tokenKeys = text.Split( new string[] { TokenDelimiter }, StringSplitOptions.RemoveEmptyEntries );
     foreach( string tokenKey in tokenKeys )
     {
       var para = _rtb.CaretPosition.Paragraph;
       var token = new Token( TokenDelimiter )
       {
         TokenKey = tokenKey,
         Item = ResolveItemByTokenKey( tokenKey )
       };
       para.Inlines.Add( CreateTokenContainer( token ) );
     }
   }
 }