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 Paul,
Thank you for your post. I have been looking into it and I suggest you set the FieldSettings’ Width to Auto, so that the AutoSizeOptions work correctly. Here you can read more about the Property:
http://help.infragistics.com/Help/NetAdvantage/WPF/2010.1/CLR3.5/html/Infragistics3.Wpf.DataPresenter.v10.1~Infragistics.Windows.DataPresenter.FieldSettings~AutoSizeOptions.html
Also I can say that when you set the AutoSizeOptions to Label, the HeaderText can’t be wrapped. Please let me know if this helps you or if I misunderstood you in some way, could you please send me an isolated sample project, where your issue is reproduced, so I can investigate it further for you.
Looking forward for your reply.
Hi Stefan,
I took your advice and set the FieldSettings' Width to Auto. As for the Label AutoSizeOption, I tried the following two scenarios:
Scenario 1: FieldSettings' Width to Auto, AutoSizeOptions = DataCells
Under this scenario, the grid columns DO resize tightly to the content of the column data cells. However, the side effect is that many of my column headers are not visible because the column width resizes to a width less than the width of the header label
Scenario 2: FieldSettings' Width to Auto, AutoSizeOptions = DataCells | Label
Under this scenario, I expect that none of my header labels can wrap. Although this is the case for most of my columns, some still wrap. I can see that if the data cell value width exceeds the width of my header label, the column does size to the width of the data cell values. But if the data cell value width is small than the width of my header label, the label does not wrap and the column does not resize to be as small as possible.
What I'm looking for is to be able to do the following:
Size the column width to be as small as possible but large enough to show the full data cell values in the column
Column headers should wrap when the data cell value content has a smaller width than the header label.
For example:
HEADER LABEL : My Column Name : ROW DATA VALUE : Red : ROW DATA VALUE : Green :
should look like this
HEADER LABEL : My : : Column : : Name : ROW DATA VALUE : Red : ROW DATA VALUE : Green :
and...
HEADER LABEL : My Column Name : ROW DATA VALUE : Red Red Red Red Red : ROW DATA VALUE : Green Green Green Green :
should remain as is.
Any ideas?
Thanks,
Paul
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.).
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.
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.
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
Hello,
Thank you for your feedback. I believe that other community members may benefit from this as well.
Thanks again.