I'm using XamDataGrid from version 10.3.20103.1003 of the WPF control set. In my grid, I need column headers to be 12pt and bold with text wrapping. The following code accomplishes this for me:
grid.FieldLayoutSettings.HeaderLabelAreaStyle = new Style(typeof(HeaderLabelArea));grid.FieldLayoutSettings.HeaderLabelAreaStyle.Setters.Add(new Setter(HeaderLabelArea.FontSizeProperty, 12d));grid.FieldLayoutSettings.HeaderLabelAreaStyle.Setters.Add(new Setter(HeaderLabelArea.FontWeightProperty, FontWeights.Bold));grid.FieldSettings.LabelTextWrapping = TextWrapping.WrapWithOverflow;
Additionally, I need the grid columns to auto-size base on the header label and cell values. The following code accomplishes this:
grid.FieldSettings.AutoSizeScope = FieldAutoSizeScope.RecordsInView;grid.FieldSettings.AutoSizeOptions = FieldAutoSizeOptions.DataCells | FieldAutoSizeOptions.Label;
The problem I'm having is that when a column header label wraps, the column doesn't resize to the smallest possible width based on the wrapped header label and cell value. When the column header label does on wrap, the auto-size feature works as expected.
Is this a known issue and is there a workaround?
Thanks,Paul
Hello,
Thank you for your feedback. I believe that other community members may benefit from this as well.
Thanks again.
This won't solve your issue but I found this in one of the forums a style you can use that will display the text with an ellipse so the user can hover over and still see the data.
<Style TargetType="{x:Type igEditors:XamTextEditor}" x:Key="XamEditorAutomaticTextTrimmingToolTip"
BasedOn="{x:Static igThemes:EditorsOffice2k7Blue
.XamTextEditor}">
<Setter Property="HorizontalContentAlignment" Value
="Stretch" />
<Setter Property
="Template">
<Setter.Value
>
<ControlTemplate TargetType="{x:Type igEditors:XamTextEditor
}">
<Border x:Name
="MainBorder"
Background="{TemplateBinding Background
}"
BorderBrush="{TemplateBinding BorderBrush
BorderThickness="{TemplateBinding BorderThickness
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels
<TextBlock x:Name="TextBlock"
Margin="{TemplateBinding Padding
Text="{TemplateBinding DisplayText}" TextWrapping
="NoWrap"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment
TextAlignment="{TemplateBinding TextAlignmentResolved}" Style="{DynamicResource TextAutoEllipseWithToolTip
}" >
</TextBlock
</Border
<ControlTemplate.Triggers
<Trigger Property="IsEmbedded" Value
="False">
<Setter TargetName="MainBorder" Property="CornerRadius" Value
="1" />
</Trigger
</ControlTemplate.Triggers
</ControlTemplate
</Setter.Value
</Setter
</Style
<Style TargetType="{x:Type TextBlock}" x:Key
="TextAutoEllipseWithToolTip">
="TextTrimming"
Value
="CharacterEllipsis"/>
<Setter Property="TextWrapping" Value
="NoWrap"/>
<Style.Triggers
<MultiTrigger
<MultiTrigger.Conditions
<Condition Property
="ctl:TextBlockService.AutomaticToolTipEnabled"
="True" />
="ctl:TextBlockService.IsTextTrimmed"
="TextWrapping"
="NoWrap" />
</MultiTrigger.Conditions
="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path
=Text}" />
</MultiTrigger
</Style.Triggers
That's exactly what I mentioned in my response - that the text could still be clipped because the textblock will not split words so if a word is longer than the available space that word will be clipped. There is no auto size mode that would size like what you described and I'm not even sure how it could be easily implemented. I mean if the grid measured the labelpresenter with the constrained size that resulted from measuring the cell data the textblock nested somewhere within the LabelPresenter would still ultimately be clipped. That is just how WPF layout works - if you measure an element with a constrained size then the size the measure returns is constrained to that size (i.e. it will never be bigger than the constraint). If we measured with a larger size then that wrapping textblock nested somwhere within the LabelPresenter would return that it wanted that space but it would continue to return a larger size until the size provided is wider than the text when shown on a single line - i.e. it would never stop returning a larger size until it was going to show it on one line. The caller has no idea how the elements nested within are using that space so the grid would have no way to know that it should stop at a certain point. If you can find a way figure out what the minimum size you need then you can set the MinWidth of the FieldSettings.
Hi Andrew,
I took your advice and used scenario 1 with the FieldSettings -> AllowLabelVirtualization property set to false. Its still not working as I would expect. I've attached a screenshot to give an example of what my grid columns look like. I need to be able to see the column header labels but have the column widths as small as possible.
You should use scenario 1 and also set the DataGrid's FieldSettings->AllowLabelVirtualization to false. Note, it is still possible for some text to be clipped. The WPF TextBlock is doing the wrapping and if a single word is longer than the longest value then I believe that the textblock doesn't break the word so that word would just get clipped. So in your example the word "Column" is likely to be wider than "Green" so part of that word could get clipped. This could even happen if the longest word in the header is shorter when the LabelPresenter has other elements consuming horizontal space (e.g. the pin button, summary button, etc.).