Note that there are some explanatory texts on larger screens.

plurals
  1. POAligning Text with a ContentControl using the HorizontalContentAlignment property
    primarykey
    data
    text
    <p>I am attempting to apply a "text alignment" to a ContentControl. Since the ContentControl does not have a horizontal or vertical text alignment property like the TextBlock, I am attempting to use the ContentControl's HorizontalContentAlignment property.</p> <p>My problem is that I can't get it to work with a ContentControl itself.</p> <p>In my example, I have a content control displaying "hello world" and a button displaying "change it".</p> <p>When I click the button, I set the HorizontalContentAlignment on the content control and on the button. The button's content changes, but the content control's content does not.</p> <p>Here is my XAML code:</p> <pre><code>&lt;Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;ContentControl x:Name="ctrl" Width="525"&gt; Hello World! &lt;/ContentControl&gt; &lt;Button x:Name="btn" Grid.Row="1" Content="Change It" Click="btn_Click"/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>And here is my VB.NET code for the button click event:</p> <pre><code>Class MainWindow Private Sub btn_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) If (ctrl.HorizontalContentAlignment = HorizontalAlignment.Left) Then ctrl.HorizontalContentAlignment = HorizontalAlignment.Right btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Right Else ctrl.HorizontalContentAlignment = HorizontalAlignment.Left btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Left End If ctrl.UpdateLayout() End Sub End Class </code></pre> <p>I am unable to replace my content controls with text blocks for various reasons, but I still need to be able to align the content.</p> <p>EDIT:</p> <p>While Narohi work around suggestion works, I am still confused about why the content control's HorizontalContentAlignment property doesn't align the content. </p> <p>I tried a Label control (which inherits from ContentControl) and it's HorizontalContentAlignment property properly aligns the content.</p> <p>(Edit again: I am no longer confused about this, it seems that the HorizontalContentAlignment isn't utilized properly in all cases.)</p> <p>Here is my updated XAML code:</p> <pre><code>&lt;Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;ControlTemplate x:Key="AlignmentAwareControl" TargetType="ContentControl"&gt; &lt;ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/&gt; &lt;/ControlTemplate&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;ContentControl x:Name="ctrlTxt" Grid.Row="0" Template="{StaticResource AlignmentAwareControl}" HorizontalContentAlignment="Center" Padding="0"&gt;Hello World Content Control!&lt;/ContentControl&gt; &lt;Label x:Name="ctrl" Grid.Row="1" HorizontalContentAlignment="Center" Padding="0"&gt;Hello World Label!&lt;/Label&gt; &lt;ContentControl x:Name="ctrlImg" Grid.Row="2" Template="{StaticResource AlignmentAwareControl}" HorizontalContentAlignment="Center"&gt; &lt;Image Source="C:\Users\Frinavale\Pictures\Business_Woman.jpg"/&gt; &lt;/ContentControl&gt; &lt;Button x:Name="btn" Grid.Row="3" Content="Change It" Click="btn_Click"/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Here is my updated VB.NET code:</p> <pre><code>Class MainWindow Private Sub btn_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) If (ctrl.HorizontalContentAlignment = HorizontalAlignment.Left) Then ctrlImg.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Right) ctrlTxt.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Right) ctrl.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Right) btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Right Else ctrlImg.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Left) ctrlTxt.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Left) ctrl.SetValue(ContentControl.HorizontalContentAlignmentProperty, Windows.HorizontalAlignment.Left) btn.HorizontalContentAlignment = Windows.HorizontalAlignment.Left End If ctrl.UpdateLayout() End Sub End Class </code></pre> <p>I'm looking forward to your advice,</p> <p>-Frinny</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload