-1

I am a new in WPF and my question is there a way to set width of column by the most width item in this column?

When I run my app this column looks like this

enter image description here

Word <EXISTING CALIBRATIO is shrink. This word should be shown entirely like <EXISTING CALIBRATION>

Like when drop down list is open

enter image description here

My code for this column is

<DataGridTemplateColumn Header="{x:Static res:Resources.geo_calib_folder}">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <ComboBox x:Name="Cb_geometry_calibration"
                                                  ToolTip="{x:Static res:Resources.tooltip_chosen_geometry_calibration_folder}"
                                                  PreviewMouseWheel="Cb_PreviewMouseWheel"
                                                      SelectionChanged="Cb_geometry_calibration_SelectionChanged"
                                                      ItemsSource="{Binding Path=GeoCalibrationFolders}"
                                                      SelectedItem="{Binding Path=SelectedGeoCalibrationFolder}"
>
                                                <ComboBox.ItemTemplate>
                                                    <DataTemplate>
                                                        <TextBlock 
                                                        Text="{Binding Path=UIRepresentation}" />
                                                    </DataTemplate>
                                                </ComboBox.ItemTemplate>

                                                <ComboBox.Style>
                                                    <Style TargetType="ComboBox">
                                                        <Style.Triggers>
                                                            <DataTrigger Binding="{Binding IsGeoCalibFolderBold}" 
                                                                         Value="True">
                                                                <Setter Property="FontWeight" Value="Bold"/>
                                                            </DataTrigger>
                                                        </Style.Triggers>
                                                    </Style>
                                                </ComboBox.Style>

                                                <ComboBox.ItemContainerStyle>
                                                    <Style TargetType="ComboBoxItem">
                                                        <Setter Property="FontWeight" Value="Normal"/>
                                                    </Style>
                                                </ComboBox.ItemContainerStyle>
                                            </ComboBox>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>

So, question is there a way to resize this column dynamically according to content?

P.S. I understand that I can hardcode this value like width = 300, but I would like to find a way to make it dynamically.

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121

1 Answers1

0

According to this duplicate post, you can't do it directly in XAML. It requires some code behind or attached behaviors because the combobox resolves its width prior to the retrieval of its collected items.

Tam Bui
  • 2,940
  • 2
  • 18
  • 27