Example #1
0
        /// <summary>
        /// Convert "data" to a flow document block object. If data is already a block, the return value is data recast.
        /// </summary>
        /// <param name="dataContext">only used when bindable content needs to be created</param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Block ConvertToBlock(object dataContext, object data)
        {
            if (data is Block)
                return (Block)data;
            else if (data is Inline)
                return new Paragraph((Inline)data);
            else if (data is BindingBase)
            {
                var run = new Run();

                if (dataContext is BindingBase)
                    run.SetBinding(Run.DataContextProperty, (BindingBase)dataContext);
                else
                    run.DataContext = dataContext;

                run.SetBinding(Run.TextProperty, (BindingBase)data);
                return new Paragraph(run);
            }
            else
            {
                var run = new Run();
                run.Text = (data == null) ? string.Empty : data.ToString();
                return new Paragraph(run);
            }
        }
        internal static Block ConvertToBlock(object dataContext, object data)
        {
            if (data is Block)
            {
                return (Block)data;
            }
            else if (data is Inline)
            {
                return new Paragraph((Inline)data);
            }
            else if (data is BindingBase)
            {
                Run run = new Run();

                if (dataContext is BindingBase)
                {
                    run.SetBinding(Run.DataContextProperty, (BindingBase)dataContext);
                }
                else
                {
                    run.DataContext = dataContext;
                }

                run.SetBinding(Run.TextProperty, (BindingBase)data);

                return new Paragraph(run);
            }
            else
            {
                Run run = new Run();
                run.Text = (data == null) ? String.Empty : data.ToString();
                return new Paragraph(run);
            }
        }
Example #3
0
 protected void SetPlaceholder() {
     Inlines.Clear();
     var placeholder = Placeholder;
     if (!string.IsNullOrEmpty(placeholder)) {
         var inline = new Run { Text = placeholder };
         inline.SetBinding(TextElement.ForegroundProperty, new Binding {
             Path = new PropertyPath(nameof(Foreground)),
             Source = this,
             Converter = this
         });
         Inlines.Add(inline);
     }
 }
 public Run CreateTextBlock(string text, FontWeight weight, DependencyProperty foreground)
 {
     Run x = new Run (text);
     x.FontWeight = weight;
     if (foreground != null)
         x.SetBinding (Run.ForegroundProperty, CreateBinding (this, foreground.Name, BindingMode.OneWay));
     return x;
 }
Example #5
0
 protected override Inline BuildElementInternal()
 {
     Run buildElementInternal;
     if (textBinding != null)
     {
         buildElementInternal = new Run();
         buildElementInternal.SetBinding(Run.TextProperty, textBinding);
     }
     else
         buildElementInternal = new Run(Text);
     if (style != null)
         buildElementInternal.SetResourceReference(FrameworkContentElement.StyleProperty, style);
     return buildElementInternal;
 }
        /// <summary>
        /// Convert "data" to a flow document block object. If data is already a block, the return value is data recast.
        /// </summary>
        /// <param name="dataContext">only used when bindable content needs to be created</param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Block ConvertToBlock(object dataContext, object data)
        {
            var block = data as Block;
            if (block != null) return block;
            var inline = data as Inline;
            if (inline != null) return new Paragraph(inline);

            var bindingBase = data as BindingBase;
            if (bindingBase != null)
            {
                var run = new Run();
                var dataContextBindingBase = dataContext as BindingBase;
                if (dataContextBindingBase != null)
                    run.SetBinding(DataContextProperty, dataContextBindingBase);
                else
                    run.DataContext = dataContext;
                run.SetBinding(Run.TextProperty, bindingBase);
                return new Paragraph(run);
            }
            return new Paragraph(new Run { Text = (data == null) ? string.Empty : data.ToString() });
        }
        private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Label lbl = d as Label;
            TextBlock block = d as TextBlock;

            if (lbl != null)
            {
                block = lbl.Content as TextBlock;

                if (block == null)
                {
                    string lblChild = lbl.Content as string;
                    TextBlock newChild = new TextBlock { Text = lblChild ?? "" };
                    lbl.Content = newChild;
                    block = newChild;
                }
            }

            if (block == null)
            {
                return;
            }

            string searchText = GetHighlightText(d);
            string blockText = GetSourceText(d);

            if (blockText == null)
            {
                return;
            }

            int last = 0;
            block.Inlines.Clear();

            if (!string.IsNullOrEmpty(searchText))
            {
                IReadOnlyList<PackageSearchUtil.Range> matches = PackageSearchUtil.ForTerm(searchText).GetMatchesInText(blockText);

                for (int i = 0; i < matches.Count; ++i)
                {
                    if (matches[i].Length == 0)
                    {
                        continue;
                    }

                    if (last < matches[i].Start)
                    {
                        string inserted = blockText.Substring(last, matches[i].Start - last);
                        block.Inlines.Add(inserted);
                        last += inserted.Length;
                    }

                    Run highlight = new Run(matches[i].ToString());
                    highlight.SetBinding(FrameworkContentElement.StyleProperty, new Binding
                    {
                        Mode = BindingMode.OneWay,
                        Source = d,
                        Path = new PropertyPath(HighlightStyleProperty)
                    });
                    block.Inlines.Add(highlight);
                    last += matches[i].Length;
                }
            }

            if (last < blockText.Length)
            {
                block.Inlines.Add(blockText.Substring(last));
            }
        }