Skip to content

Instantly share code, notes, and snippets.



Last active Dec 22, 2016
What would you like to do?
WPF Content control ignoring the content width. Suitable to force a TextBlock to wrap the text.
<!-- Especially usefull when placed inside Window and ScrollViewer -->
<TextBlock Text="Very long text which has to be wrapped. Yeah, it must be wrapped." TextWrapping="Wrap" />
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace MyControls
/// <summary>
/// Content control ignoring the content width and setting the content MaxWidth to the actual width.
/// Suitable to force a TextBlock to wrap the text.
/// </summary>
public class IgnoreWidthControl : ContentControl
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
if (sizeInfo.WidthChanged)
protected override Size MeasureOverride(Size constraint)
constraint.Width = ActualWidth;
Size size = new Size();
UIElement child = GetFirstVisualChild();
if (child != null)
size.Height = child.DesiredSize.Height;
return size;
private UIElement GetFirstVisualChild()
if (this.VisualChildrenCount <= 0)
return null;
return this.GetVisualChild(0) as UIElement;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.