I have a checkbox located in a grid with a column span of 3, where 2 of the columns are resizable, and can be resized during runtime.
I want the checkbox to show ellipsis when its text doesn't fit, but can't get this to work.
Here is my XAML code.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
...
<CheckBox IsEnabled="False" Grid.Row="2" Padding="5" Margin="11,12,0,0" Name="chkSelectedCat" VerticalAlignment="Top" HorizontalAlignment="Stretch" Grid.ColumnSpan="3">
<AccessText Text="Search in selected category only." TextTrimming="CharacterEllipsis"/>
</CheckBox>
Thanks in advance for your help.
EDIT
The CheckBox
in question is located within a Grid
, which is in turn contained within a GroupBox
, which is in a column adjacent to a column which is resized using a GridSplitter
.
The problem was that the
GroupBox
allots infinite width to its child controls, as discussed at this MSDN forum thread.In fact, when setting a
MaxWidth
to theCheckBox
, text trimming worked and the ellipsis were displayed.However the
CheckBox
maximum width needed to be changed during runtime.I tried using a MultiValueConverter to bind the
MaxWidth
of theCheckBox
to the totalActualWidth
of the three columns, but theConvert
method was only being called during initialisation (again I think this is something to do with how theGroupBox
assigns sizes to its child controls).Anyway I managed to achieve what I wanted by removing the
ChechkBox
'sAccessText
and using aTextBlock
in aStackPanel
in its stead, and then surrounding everything with aBorder
.When the size of the
Border
is changed, I update theWidth
of the StackPanel in the event handler, and the text in theTextBox
trims accordingly.Hope this helps someone in the future (even though it is sort of a hack).
UPDATE
If you can eliminate the
StackPanel
and use aGrid
instead,TextTrimming
works automatically, without having to do any event handling, as shown below.Make sure to set the
Width
of the column containing the textbox to"*"
, because"Auto"
basically tells theTextBox
that it can have as much space as it wants, soTextTrimming
would not work.