Hello,
in a XamDatagrid there are a few colums that should autofit there width and use all available space. And there are some others that should stays static or resize on their own.
1. By resizing the autofit colums they use the availble space equaly
2. If one of the other colums are resized, the autofit colums need to do their resize again
After searching in the forums I found following posting https://www.infragistics.com/community/forums/f/ultimate-ui-for-winui/122468/xamdatagrid-disable-columns-autofit/539886#539886 but could not apply it. XamDatagrid (derived from DataPresenter) does not offer this setting.
Please advice
Thanks
Niko
Hello Niko,
It is possible to define or exclude columns from the AutoFit functionality of the XamDataGrid in WPF by setting the Width property of the Field. By default, this will be set to “Auto” and so if an AutoFit functionality is on, the Field will respect that. If you define a manual size the Field though, it will not be auto-fit.
Regarding the forum thread that you provided, the “XamDataGrid” there is not the WPF XamDataGrid. Our WinUI’s data grid is also named the XamDataGrid, but it is a completely different implementation and API.
Please let me know if you have any other questions or concerns on this matter.
Hello Andrew,
please take a look at the XAML (only partial) below. I have removed Styles etc.
Then main issues is on th first field: This should be excluded from AutoFit().
The XAML below does Autofi(), but for all fields including the first one.
Please let me know if you see something, that could cause the problem.
<igDP:XamDataGrid Name="BaustellenEinteilung" SelectedDataItemsScope="RecordsOrCells" DataSource="{Binding Baustellen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" GroupByAreaLocation="AboveDataArea" AutoFit="True" AllowDrop="True" InitializeRecord="RefreshSorting" ScrollingMode="Immediate" SelectedDataItem="{Binding CurrentBaustelle}"> <igDP:XamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings AllowAddNew="False" AllowDelete="False" AutoGenerateFields="False" HeaderPrefixAreaDisplayMode="FieldChooserButton" RecordSelectorLocation="None" HeaderPlacement="OnTopOnly" SelectionTypeCell="Extended" AutoFixMode="Always"/> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.FieldSettings> <igDP:FieldSettings AllowRecordFiltering="True" FilterStringComparisonType="CaseInsensitive" FilterOperatorDefaultValue="Contains" FilterOperandUIType="ExcelStyle" FilterOperatorDropDownItems="None" FilterClearButtonVisibility="Collapsed" IsAutoEditModeEnabled="True" ExpandableFieldRecordExpansionMode="Default" LabelTextAlignment="Center" LabelTextWrapping="Wrap" LabelClickAction="SortByMultipleFieldsTriState" CellClickAction="SelectCell"/> </igDP:XamDataGrid.FieldSettings> <igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout> <!-- This field should be excluded from Autofit--> <igDP:Field AllowHiding="Never" BindingType="Unbound" IsReadOnly="True" AllowEdit="False" DisallowModificationViaClipboard="True" Width="30" AllowResize="False" AllowGroupBy="True" AllowRecordFiltering="False" FixedLocation="FixedToNearEdge" CellValuePresenterStyle="{StaticResource BlankStyle}"> <igDP:Field.Settings> <igDP:FieldSettings LabelClickAction="Nothing"> <igDP:FieldSettings.LabelPresenterStyle> <Style TargetType="igDP:LabelPresenter"> <EventSetter Event="PreviewMouseLeftButtonUp" Handler="ListConfigButton_PreviewMouseLeftButtonUp" /> <EventSetter Event="PreviewMouseMove" Handler="PreventColumnMove" /> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <ContentControl x:Name="icon" ToolTip="Baustellen konfigurieren" Content="{Binding Source={StaticResource ListConfigIcon}}"/> </DataTemplate> </Setter.Value> </Setter> <Setter Property="Background" Value="White" /> <Setter Property="MinWidth" Value="30"/> <Setter Property="MaxWidth" Value="30"/> </Style> </igDP:FieldSettings.LabelPresenterStyle> </igDP:FieldSettings> </igDP:Field.Settings> </igDP:Field><!-- This field should be part of Autofit--> <igDP:TemplateField Label="Bauleitung" Name="Bauleitung" AlternateBinding="{Binding Bauleitung}" IsReadOnly="True" AllowEdit="False" DisallowModificationViaClipboard="True" CellValuePresenterStyle="{StaticResource NichtTagStyle}" Visibility="Collapsed" DisplayTemplate="{StaticResource StammdatenmitLinie}"/>
My apologies, it looks like I had misunderstood how the AutoFit is working under the hood. The actual way to exclude Fields in the XamDataGrid from auto fitting is to set the AutoFitMode to “OnlyWithVisibleStarFields.” This will require you to set the Width property of the Fields to “*” in order to essentially “opt-in” to the AutoFit. Fields without this setting will not be considered for auto fit.
I can confirm that the AutofitMode=OnlyWithVisibleStarFields" and Width"*" works as you decribed - thansk for that.
Due to the fact that I try to keep those autofit-columns in sync in terms of width I guess I hit two not compatible targets:
1. The selected colums Width="*" should autofit
2. The very same columns should always have the same width (code behind checking for changes of column or label width events.
At the moment I can choose between Autofit or equal width of the selected columns.
Please let me know if you see a way forward from this tricky situation. I guess it would lead to several cycles of resizing.
If the available width of the XamDatagrid is know as well as the width of all visible columns I could divide this space by the number of coumns that should have the same width and apply it.
I am a little unsure if I understand your most recent requirement in this case, but I will try to explain the “*” sizing with “OnlyWithVisibleStarFields” in the XamDataGrid.
For this example, let’s say we have Fields A, B, C, and D. If the XamDataGrid has a Width of around 330 pixels and Field A has a Width of 30, then if B, C, and D all have “*” as their widths, they should each be 100 pixels wide. Where this can get derailed is with the resizing of the Fields, as if you resize a “*” Field, it will break out of the AutoFit. So, if you were to resize Field A in this case, then B, C, and D will still be AutoFit to the same widths, but if you were to then resize Field B, then Fields C and D would be sized at the same width. Field B would then be out of the “AutoFit.”
I hope this helps. Please let me know if you have any other questions or concerns on this matter.