Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text on the left side of checkbox in WPF?

What is the easiest way to put checkbox content (text) on the left side of the checkbox itself?

like image 219
Ivan Peric Avatar asked Jul 04 '13 08:07

Ivan Peric


2 Answers

A solution that maximizes "easiness" and "correctness" is to make a RightToLeft checkbox with LeftToRight content:

<CheckBox FlowDirection="RightToLeft">     <TextBlock FlowDirection="LeftToRight" Text="CheckBox Content:" /> </CheckBox> 

Or if you'd like a style:

<Style TargetType="CheckBox">     <Setter Property="FlowDirection" Value="RightToLeft" />     <Setter Property="ContentTemplate">         <Setter.Value>             <DataTemplate>                 <ContentControl FlowDirection="LeftToRight" Content="{Binding}" />             </DataTemplate>         </Setter.Value>     </Setter> </Style> 
like image 134
nmclean Avatar answered Oct 15 '22 01:10

nmclean


In code:

System.Windows.Controls.CheckBox checkBox = new System.Windows.Controls.CheckBox(); checkBox.Content = ":CheckBox Enabled"; checkBox.FlowDirection = System.Windows.FlowDirection.RightToLeft; 

In XAML:

<CheckBox FlowDirection="RightToLeft" Content=":CheckBox Enabled" /> 

EDIT

User punker76 helped me notice that colon ":" has to be places infront of the text to be displayed correctly, at the end ("CheckBox Enabled:"), probably caused by an affect flow direction has on text element. Nice catch.

like image 31
Ivan Peric Avatar answered Oct 14 '22 23:10

Ivan Peric