Although I suspect there may be a better way to solve your problem, I think I have an answer for what you want to do. ( You didn't mention what type your container is. A StackPanel for instance takes care of the width calculation for you. See TextBox#2 below)
First the XAML
<Window x:Class="WpfApplication1.Window2" ...
xmlns:local="clr-namespace:WpfApplication1"
Title="Window2" Height="300" Width="300">
<Window.Resources>
<local:WidthSansMarginConverter x:Key="widthConverter" />
</Window.Resources>
<Grid>
<StackPanel x:Name="stack">
<TextBlock x:Name="txtStatusMessages"
Width="{Binding ElementName=stack,Path=ActualWidth,
Converter={StaticResource widthConverter}}"
TextWrapping="WrapWithOverflow"
Background="Aquamarine"
Margin="5,5,5,5">
This is a message
</TextBlock>
<TextBlock x:Name="txtWhatsWrongWithThis"
TextWrapping="WrapWithOverflow"
Background="Aquamarine"
Margin="5,5,5,5">
This is another message
</TextBlock>
</StackPanel>
</Grid>
</Window>
Next the Converter. We have a problem here.. since the ConverterParameter for the Convert methods cannot be a dynamic value for some reason. So we sneak in the Textbox Margin via a public property of the Converter that we set in Window's ctor.
WidthSansMarginConverter.cs
public class WidthSansMarginConverter : IValueConverter
{
private Thickness m_Margin = new Thickness(0.0);
public Thickness Margin
{
get { return m_Margin; }
set { m_Margin = value; }
}
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(double)) { return null; }
double dParentWidth = Double.Parse(value.ToString());
double dAdjustedWidth = dParentWidth-m_Margin.Left-m_Margin.Right;
return (dAdjustedWidth < 0 ? 0 : dAdjustedWidth);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Window2.xaml.cs
public Window2()
{
InitializeComponent();
WidthSansMarginConverter obConverter = this.FindResource("widthConverter") as WidthSansMarginConverter;
obConverter.Margin = txtStatusMessages.Margin;
}
HTH. Thanks for the exercise :)