Say I have a TextBlock that looks like this:
<TextBlock TextWrapping="Wrap" >
Text in <Run Foreground="Red">red</Run> and <Run Foreground="Red">more</Run>
</TextBlock>
That works fine and the text is shown in red as you think it would. However, in my real example this text:
Text in <Run Foreground="Red">red</Run> and <Run Foreground="Red">more</Run>
Comes from a service call. (Including the tags.)
Is there a way to supply this text to the TextBlock at runtime?
In fact you have to convert all the input text into Inlines collection (of type TextElementCollection). However doing so requires parsing the input text yourself which is not easy. So why not use the existing parser supported by XamlReader. By using this class, you can parse a XAML code to get an instance of a TextBlock from which you can get the Inlines collection and add that for your actual TextBlock. Here is the code:
var text = "<TextBlock>Text in <Run Foreground=\"Red\">red</Run> and <Run Foreground=\"Red\">more</Run></TextBlock>";
var pc = new System.Windows.Markup.ParserContext();
pc.XmlnsDictionary[""] = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
pc.XmlnsDictionary["x"] = "http://schemas.microsoft.com/winfx/2006/xaml";
var tbl = System.Windows.Markup.XamlReader.Parse(text, pc) as TextBlock;
yourTextBlock.Inlines.Clear();
yourTextBlock.Inlines.AddRange(tbl.Inlines.ToList());
Your actual input text should be wrapped in the pair <TextBlock> & </TextBlock> before being used by the next code.
I thought I would post the control that I made from King Kong's answer. (Just in case it is useful to someone else.)
public class FormatBindableTextBlock : TextBlock
{
private static readonly ParserContext parserContext;
static FormatBindableTextBlock()
{
parserContext = new System.Windows.Markup.ParserContext();
parserContext.XmlnsDictionary[""] = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
parserContext.XmlnsDictionary["x"] = "http://schemas.microsoft.com/winfx/2006/xaml";
}
public static readonly DependencyProperty FormattedContentProperty =
DependencyProperty.Register("FormattedContent", typeof(string), typeof(FormatBindableTextBlock), new UIPropertyMetadata(null, OnFormattedContentPropertyChanged));
public string FormattedContent
{
get { return (string)GetValue(FormattedContentProperty); }
set { SetValue(FormattedContentProperty, value); }
}
private static void OnFormattedContentPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
FormatBindableTextBlock textBlock = sender as FormatBindableTextBlock;
if (textBlock == null)
return;
string newFormattedContent = e.NewValue as string;
if (string.IsNullOrEmpty(newFormattedContent))
newFormattedContent = "";
newFormattedContent = "<TextBlock>" + newFormattedContent + "</TextBlock>";
TextBlock tbl = System.Windows.Markup.XamlReader.Parse(newFormattedContent, parserContext) as TextBlock;
textBlock.Inlines.Clear();
textBlock.Inlines.AddRange(tbl.Inlines.ToList());
}
}
Related
In WPF(.Net 4.5) I have RichTextBox in my xaml
<RichTextBox Height="40" Width="100">
<FlowDocument>
<Paragraph>
<Run Text="{Binding TestRichTextBox}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
in and ViewModel I have this Property
string testRichTextBox;
public string TestRichTextBox
{
get { return testRichTextBox; }
set
{
if (testRichTextBox == value)
return;
testRichTextBox = value;
onPropertyChanged("TestRichTextBox");
}
}
scenario :
RichTextBox has content
select all text (ctrl+A)
paste new text
in ViewModel I have breakpoint on set method, new Value is empty string.
Is this normal behavior or a bug?
Could you please try this and check if var text is returning any value?
public Window1()
{
InitializeComponent();
// "tb" is a richtextBox
DataObject.AddPastingHandler(tb, OnPaste);
}
private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
if (!isText) return;
var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
...
}
I am doing some localization and have bumped into a problem that I cant seem to figure out.
I need the following displayed: tcpCO₂ (where tcp is in italic)
<Italic>tcp</Italic> CO₂
I thought I could just put the HTML in my .resx file and all would be marry, but the content of the output shows the html including brackets etc. Does anyone have inputs in this matter?
I use an attached behavior to display rich XAML text in a TextBlock:
public static class TextBlockBehavior
{
[AttachedPropertyBrowsableForType(typeof(TextBlock))]
public static string GetRichText(TextBlock textBlock)
{
return (string)textBlock.GetValue(RichTextProperty);
}
public static void SetRichText(TextBlock textBlock, string value)
{
textBlock.SetValue(RichTextProperty, value);
}
public static readonly DependencyProperty RichTextProperty =
DependencyProperty.RegisterAttached(
"RichText",
typeof(string),
typeof(TextBlockBehavior),
new UIPropertyMetadata(
null,
RichTextChanged));
private static void RichTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
TextBlock textBlock = o as TextBlock;
if (textBlock == null)
return;
var newValue = (string)e.NewValue;
textBlock.Inlines.Clear();
if (newValue != null)
AddRichText(textBlock, newValue);
}
private static void AddRichText(TextBlock textBlock, string richText)
{
string xaml = string.Format(#"<Span>{0}</Span>", richText);
ParserContext context = new ParserContext();
context.XmlnsDictionary.Add(string.Empty, "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
var content = (Span)XamlReader.Parse(xaml, context);
textBlock.Inlines.Add(content);
}
}
You can use it like this:
<TextBlock bhv:TextBlockBehavior.RichText="{x:Static Resources:Resource.CO2}">
However, in your case don't have access to the TextBlock directly, since it's in the caption template of the LayoutPanel. So you need to redefine this template to apply the behavior on the TextBlock:
<dxdo:LayoutPanel Name="PanelCo2" Caption="{x:Static Resources:Resource.CO2}">
<dxdo:LayoutPanel.CaptionTemplate>
<DataTemplate>
<TextBlock bhv:TextBlockBehavior.RichText="{Binding}">
</DataTemplate>
</dxdo:LayoutPanel.CaptionTemplate>
</dxdo:LayoutPanel>
I'm using Silverlight on Windows Phone 7.
I want to display the first part of some text in a TextBlock in bold, and the rest in normal font. The complete text must wrap. I want the bolded part to contain text from one property in my ViewModel, and the plain text to contain text from a different property.
The TextBlock is defined in a DataTemplate associated with a LongListSelector.
My initial attempt was:
<TextBlock TextWrapping="Wrap">
<TextBlock.Inlines>
<Run Text="{Binding Property1}" FontWeight="Bold"/>
<Run Text="{Binding Property2}"/>
</TextBlock.Inlines>
</TextBlock>
This fails at runtime with the spectacularly unhelpful "AG_E_RUNTIME_MANAGED_UNKNOWN_ERROR". This is a known issue because the Run element is not a FrameworkElement and cannot be bound.
My next attempt was to put placeholders in place, and then update them in code:
<TextBlock Loaded="TextBlockLoaded" TextWrapping="Wrap">
<TextBlock.Inlines>
<Run FontWeight="Bold">Placeholder1</Run>
<Run>Placeholder2</Run>
</TextBlock.Inlines>
</TextBlock>
In the code-behind (yes I am desparate!):
private void TextBlockLoaded(object sender, RoutedEventArgs e)
{
var textBlock = (TextBlock)sender;
var viewModel = (ViewModel)textBlock.DataContext;
var prop1Run = (Run)textBlock.Inlines[0];
var prop2Run = (Run)textBlock.Inlines[1];
prop1Run.Text = viewModel.Property1;
prop2Run.Text = viewModel.Property2;
}
This seemed to work, but because I am using the LongListSelector, although items get recycled, the Loaded codebehind event handler doesn't re-initialize the Runs, so very quickly the wrong text is displayed...
I've looked at using the LongListSelector's Linked event (which I already use to free up images that I display in the list), but I can't see how I can use that to re-initialize the Runs' text properties.
Any help appreciated!
I finally found a solution that works for me.
As I mention in the comment, Paul Stovell's approach would not work.
Instead I used a similar approach to add an attached property to the TextBlock, bound to the TextBlock's DataContext, and attached properties on the runs, indicating which ViewModel properties they should be bound to:
<TextBlock TextWrapping="Wrap"
Views:BindableRuns.Target="{Binding}">
<TextBlock.Inlines>
<Run FontWeight="Bold" Views:BindableRuns.Target="Property1"/>
<Run Views:BindableRuns.Target="Property2"/>
</TextBlock.Inlines>
</TextBlock>
Then in my attached TextBox Target (datacontext) property's changed event, I update the Runs, and subscribe to be notified of changes to the TextBox Target properties. When a TextBox Target property changes, I updated any associated Run's text accordingly.
public static class BindableRuns
{
private static readonly Dictionary<INotifyPropertyChanged, PropertyChangedHandler>
Handlers = new Dictionary<INotifyPropertyChanged, PropertyChangedHandler>();
private static void TargetPropertyPropertyChanged(
DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
{
if(!(dependencyObject is TextBlock)) return;
var textBlock = (TextBlock)dependencyObject;
AddHandler(e.NewValue as INotifyPropertyChanged, textBlock);
RemoveHandler(e.OldValue as INotifyPropertyChanged);
InitializeRuns(textBlock, e.NewValue);
}
private static void AddHandler(INotifyPropertyChanged dataContext,
TextBlock textBlock)
{
if (dataContext == null) return;
var propertyChangedHandler = new PropertyChangedHandler(textBlock);
dataContext.PropertyChanged += propertyChangedHandler.PropertyChanged;
Handlers[dataContext] = propertyChangedHandler;
}
private static void RemoveHandler(INotifyPropertyChanged dataContext)
{
if (dataContext == null || !Handlers.ContainsKey(dataContext)) return;
dataContext.PropertyChanged -= Handlers[dataContext].PropertyChanged;
Handlers.Remove(dataContext);
}
private static void InitializeRuns(TextBlock textBlock, object dataContext)
{
if (dataContext == null) return;
var runs = from run in textBlock.Inlines.OfType<Run>()
let propertyName = (string)run.GetValue(TargetProperty)
where propertyName != null
select new { Run = run, PropertyName = propertyName };
foreach (var run in runs)
{
var property = dataContext.GetType().GetProperty(run.PropertyName);
run.Run.Text = (string)property.GetValue(dataContext, null);
}
}
private class PropertyChangedHandler
{
private readonly TextBlock _textBlock;
public PropertyChangedHandler(TextBlock textBlock)
{
_textBlock = textBlock;
}
public void PropertyChanged(object sender,
PropertyChangedEventArgs propertyChangedArgs)
{
var propertyName = propertyChangedArgs.PropertyName;
var run = _textBlock.Inlines.OfType<Run>()
.Where(r => (string) r.GetValue(TargetProperty) == propertyName)
.SingleOrDefault();
if(run == null) return;
var property = sender.GetType().GetProperty(propertyName);
run.Text = (string)property.GetValue(sender, null);
}
}
public static object GetTarget(DependencyObject obj)
{
return obj.GetValue(TargetProperty);
}
public static void SetTarget(DependencyObject obj,
object value)
{
obj.SetValue(TargetProperty, value);
}
public static readonly DependencyProperty TargetProperty =
DependencyProperty.RegisterAttached("Target",
typeof(object),
typeof(BindableRuns),
new PropertyMetadata(null,
TargetPropertyPropertyChanged));
}
I suggest you give the BindableRun a try. I've only used it in WPF, but I don't see why it wouldn't work in Silverlight.
I would like to do the equivalent of the following xaml in code, but I don't know how to grab that text block element:
<local:DayOfTheWeekColumn
...
<local:DayOfTheWeekColumn.Header>
<TextBlock
Text="{Binding ...},
ToolTip="{Binding ...} />
</local:DayOfTheWeekColumn.Header>
</local:DayOfTheWeekColumn>
The DayOfTheWeekColumn is a subclass of a DataGridTextColumn. I can get at the Header easily enough and set it's content, and now I want to set the ToolTip in code, figuring the way to do that is just how I am doing it in the xaml above.
Cheers,
Berryl
EDIT =========
Here is the code, so far, for the DayOfTheWeekColumn. The TextBlock in the xaml is part of the Header's visual tree, and not something I want to keep in the xaml. I do want to access it's toolTip though, in code, so I can set it there.
I am thinking there should be a Children property on the column Header that I can access to find the TextBlock, but haven't found that yet.
public class DayOfTheWeekColumn : DataGridTextColumn
{
public static readonly DependencyProperty DowDateProperty = DependencyProperty.RegisterAttached(
"DowDate", typeof (DateTime), typeof (DayOfTheWeekColumn), new PropertyMetadata(OnDateChanged));
public DateTime DowDate
{
get { return (DateTime)GetValue(DowDateProperty); }
set { SetValue(DowDateProperty, value); }
}
private static void OnDateChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) {
var col = (DataGridTextColumn) target;
var date = (DateTime) e.NewValue;
col.Header = date.ToString(Strings.ShortDayOfWeekFormat);
//col.Header.ToolTip = "If Only It Were so Easy!!" <==============
}
public DayOfTheWeekColumn() {
Width = 60;
CanUserReorder = false;
CanUserResize = false;
CanUserSort = false;
}
}
p161 of Chris Andersen's Essential Windows Presentation Foundation pretty much answers this question. If you have it, I recommend it as a reference.
However, you are so close I am not sure how you missed it :)
private static void OnDateChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) {
var col = (DataGridTextColumn) target;
var date = (DateTime) e.NewValue;
var textblock = new TextBlock();
col.Header = textblock;
textblock.Text = date.ToString(Strings.ShortDayOfWeekFormat);
textblock.ToolTip = "It is that easy. :)";
}
Give your TextBlock a name with the x:Name attribute and you should be able to access it in code by that name.
<TextBlock x:Name="textBlock1" />
The following textblock wraps and trims as expected. The elipsis "..." is displayed when the text is trimmed.
<TextBlock
MaxWidth="60"
MaxHeight="60"
Text="This is some long text which I would like to wrap."
TextWrapping="Wrap"
TextTrimming="CharacterEllipsis" />
I would like to display a tooltip over the text with the full text, but only if the text is trimmed. I'm not sure how to reliably determine if the "..." is being shown or not.
How do I determine if the text is being trimmed or not?
Because the link in Alek's answer is down, I found a cached copy of the link from the wayback machine. You can not download the code linked in the article, so here is a pre-assembled version of the code. There was one or two issues I ran in to while trying to make it work so this code is slightly different then the code in the examples in the article.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace TextBlockService
{
//Based on the project from http://web.archive.org/web/20130316081653/http://tranxcoder.wordpress.com/2008/10/12/customizing-lookful-wpf-controls-take-2/
public static class TextBlockService
{
static TextBlockService()
{
// Register for the SizeChanged event on all TextBlocks, even if the event was handled.
EventManager.RegisterClassHandler(
typeof(TextBlock),
FrameworkElement.SizeChangedEvent,
new SizeChangedEventHandler(OnTextBlockSizeChanged),
true);
}
private static readonly DependencyPropertyKey IsTextTrimmedKey = DependencyProperty.RegisterAttachedReadOnly("IsTextTrimmed",
typeof(bool),
typeof(TextBlockService),
new PropertyMetadata(false));
public static readonly DependencyProperty IsTextTrimmedProperty = IsTextTrimmedKey.DependencyProperty;
[AttachedPropertyBrowsableForType(typeof(TextBlock))]
public static Boolean GetIsTextTrimmed(TextBlock target)
{
return (Boolean)target.GetValue(IsTextTrimmedProperty);
}
public static readonly DependencyProperty AutomaticToolTipEnabledProperty = DependencyProperty.RegisterAttached(
"AutomaticToolTipEnabled",
typeof(bool),
typeof(TextBlockService),
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));
[AttachedPropertyBrowsableForType(typeof(DependencyObject))]
public static Boolean GetAutomaticToolTipEnabled(DependencyObject element)
{
if (null == element)
{
throw new ArgumentNullException("element");
}
return (bool)element.GetValue(AutomaticToolTipEnabledProperty);
}
public static void SetAutomaticToolTipEnabled(DependencyObject element, bool value)
{
if (null == element)
{
throw new ArgumentNullException("element");
}
element.SetValue(AutomaticToolTipEnabledProperty, value);
}
private static void OnTextBlockSizeChanged(object sender, SizeChangedEventArgs e)
{
TriggerTextRecalculation(sender);
}
private static void TriggerTextRecalculation(object sender)
{
var textBlock = sender as TextBlock;
if (null == textBlock)
{
return;
}
if (TextTrimming.None == textBlock.TextTrimming)
{
textBlock.SetValue(IsTextTrimmedKey, false);
}
else
{
//If this function is called before databinding has finished the tooltip will never show.
//This invoke defers the calculation of the text trimming till after all current pending databinding
//has completed.
var isTextTrimmed = textBlock.Dispatcher.Invoke(() => CalculateIsTextTrimmed(textBlock), DispatcherPriority.DataBind);
textBlock.SetValue(IsTextTrimmedKey, isTextTrimmed);
}
}
private static bool CalculateIsTextTrimmed(TextBlock textBlock)
{
if (!textBlock.IsArrangeValid)
{
return GetIsTextTrimmed(textBlock);
}
Typeface typeface = new Typeface(
textBlock.FontFamily,
textBlock.FontStyle,
textBlock.FontWeight,
textBlock.FontStretch);
// FormattedText is used to measure the whole width of the text held up by TextBlock container
FormattedText formattedText = new FormattedText(
textBlock.Text,
System.Threading.Thread.CurrentThread.CurrentCulture,
textBlock.FlowDirection,
typeface,
textBlock.FontSize,
textBlock.Foreground);
formattedText.MaxTextWidth = textBlock.ActualWidth;
// When the maximum text width of the FormattedText instance is set to the actual
// width of the textBlock, if the textBlock is being trimmed to fit then the formatted
// text will report a larger height than the textBlock. Should work whether the
// textBlock is single or multi-line.
// The "formattedText.MinWidth > formattedText.MaxTextWidth" check detects if any
// single line is too long to fit within the text area, this can only happen if there is a
// long span of text with no spaces.
return (formattedText.Height > textBlock.ActualHeight || formattedText.MinWidth > formattedText.MaxTextWidth);
}
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tbs="clr-namespace:TextBlockService">
<!--
Rather than forcing *all* TextBlocks to adopt TextBlockService styles,
using x:Key allows a more friendly opt-in model.
-->
<Style TargetType="TextBlock" x:Key="TextBlockService">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="tbs:TextBlockService.AutomaticToolTipEnabled" Value="True" />
<Condition Property="tbs:TextBlockService.IsTextTrimmed" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Text}" />
</MultiTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
I haven't done a lot of WPF lately, so I'm not sure if this is what you're looking for, but check out this article: Customizing “lookful” WPF controls – Take 2. It's a bit complex, but it seems to address the same question you're asking. UPDATE: The website seems gone, but you can find the article in the archive. SEE Scott Chamberlain's ANSWER WITH THE SAMPLE CODE (thanks Scott).
The solution above didn't work for me if the TextBlock is part of a ListBoxItem DataTemplate.
I propose another solution:
public class MyTextBlock : System.Windows.Controls.TextBlock
{
protected override void OnToolTipOpening(WinControls.ToolTipEventArgs e)
{
if (TextTrimming != TextTrimming.None)
{
e.Handled = !IsTextTrimmed();
}
}
private bool IsTextTrimmed()
{
Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
return ActualWidth < DesiredSize.Width;
}
}
XAML:
<MyTextBlock Text="{Binding Text}" TextTrimming="CharacterEllipsis" ToolTip="{Binding Text}" />
Expanding on bidy's answer. This will create a TextBlock that only shows the tooltip when not all text is shown. The tooltip will be resized to the contents (as opposed to the default tooltip which will remain a one-line box with the text cut off).
using System;
using System.Windows;
using System.Windows.Controls;
namespace MyComponents
{
public class CustomTextBlock : TextBlock
{
protected override void OnInitialized(EventArgs e)
{
// we want a tooltip that resizes to the contents -- a textblock with TextWrapping.Wrap will do that
var toolTipTextBlock = new TextBlock();
toolTipTextBlock.TextWrapping = TextWrapping.Wrap;
// bind the tooltip text to the current textblock Text binding
var binding = GetBindingExpression(TextProperty);
if (binding != null)
{
toolTipTextBlock.SetBinding(TextProperty, binding.ParentBinding);
}
var toolTipPanel = new StackPanel();
toolTipPanel.Children.Add(toolTipTextBlock);
ToolTip = toolTipPanel;
base.OnInitialized(e);
}
protected override void OnToolTipOpening(ToolTipEventArgs e)
{
if (TextTrimming != TextTrimming.None)
{
e.Handled = !IsTextTrimmed();
}
}
private bool IsTextTrimmed()
{
Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
return ActualWidth < DesiredSize.Width;
}
}
}
XAML usage:
<Window ...
xmlns:components="clr-namespace:MyComponents"
... >
<components:CustomTextBlock Text="{Binding Details}" TextTrimming="CharacterEllipsis" />
I had a small issue using Alex answer and had to change my logic a bit to clarify if the text in the text block was being trimmed.
var formattedText = new FormattedText(
Text, System.Threading.Thread.CurrentThread.CurrentCulture, FlowDirection, typeface, FontSize,
Foreground, VisualTreeHelper.GetDpi( this ).PixelsPerDip ) { MaxTextWidth = ActualWidth };
//Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
return ( Math.Floor(formattedText.Height ) > ActualHeight || Math.Floor( formattedText.MinWidth ) > ActualWidth;
This works perfectly for me.
I defined a user control that was a TextBlock with ellipsis enabled. Then I defined 2 functions for OnMouseUp and OnMouseDown, so that when the user clicked on the textblock that had overflow it would display a tooltip with the full value.
This is the OnMouseDown function
private void TextBlockWithToolTipView_OnMouseDown(
object sender,
MouseButtonEventArgs e )
{
var typeface = new Typeface(
FontFamily,
FontStyle,
FontWeight,
FontStretch);
var formattedText = new FormattedText(
Text, System.Threading.Thread.CurrentThread.CurrentCulture, FlowDirection, typeface, FontSize,
Foreground, VisualTreeHelper.GetDpi( this ).PixelsPerDip ) { MaxTextWidth = ActualWidth };
if (Math.Floor(formattedText.Height) > ActualHeight || Math.Floor(formattedText.MinWidth) > ActualWidth )
{
if( ToolTip is ToolTip tt )
{
{
if( tt.PlacementTarget == null )
{
tt.PlacementTarget = this;
}
tt.IsOpen = true;
e.Handled = true;
}
}
}
}
And this was the Xaml bit
<TextBlock
ToolTipService.IsEnabled="True"
MouseDown="TextBlockWithToolTipView_OnMouseDown"
MouseLeave="TextBlockWithToolTipView_OnMouseLeave"
TextTrimming="CharacterEllipsis"
TextWrapping="WrapWithOverflow">
<TextBlock.ToolTip>
<ToolTip
DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
<TextBlock Text="{Binding Path=Text, Mode=OneWay }"
TextWrapping="Wrap"/>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>