【问题标题】:How to set width of DataGridTemplateColumn by content?如何按内容设置 DataGridTemplateColumn 的宽度?
【发布时间】:2020-06-18 01:24:42
【问题描述】:

我是 WPF 的新手,我的问题是有没有办法通过此列中最宽的项目来设置列的宽度?

当我运行我的应用程序时,此列看起来像这样

<EXISTING CALIBRATIO 正在缩小。这个词应该完全像<EXISTING CALIBRATION>一样显示

就像打开下拉列表时一样

我的专栏代码是

<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>

那么,请问有没有办法根据内容动态调整此列的大小?

附:我知道我可以像width = 300 这样对这个值进行硬编码,但我想找到一种动态生成它的方法。

【问题讨论】:

  • 你试过设置Width=Auto吗?类似&lt;DataGridTemplateColumn Width="Auto"
  • @trix 它没有帮助......它通过标题设置宽度......据我了解
  • 是否应用了任何样式?
  • @AlekseyTimoshchenko:你有没有尝试在一个新的空白应用程序中重现这个?

标签: c# wpf


【解决方案1】:

根据这个duplicate post,你不能直接在XAML中做。它需要一些代码隐藏或附加行为,因为组合框在检索其收集的项目之前解析其宽度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 2013-10-16
    • 2012-09-27
    • 1970-01-01
    • 2022-09-28
    • 2017-02-01
    • 2021-11-26
    相关资源
    最近更新 更多