列中的内容更改时展开datagrid的容器

时间:2013-02-12 22:18:05

标签: c# wpf xaml

我有一个带有3列的DataGrid。第一列单元格是Combo,用户可以从中选择不同长度的值。我希望第一列显示所选项目的整个值(即它应该适合其内容的大小)。这在我设置列的宽度=“自动”时有效。问题是在某个时刻,当网格的整个宽度被占用时,滚动条出现。相反,我宁愿数据网格本身总是扩展,因此它的容器。我怎样才能做到这一点.. 我的xaml的结构如下

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <local:MyUserControl... Grid.Col="1">
    <Expander Grid.Column="2">
        <GroupBox  Template="{Binding OptionsSelectorTemplate}" Grid.Row="1" />
    </Expander
</Grid>

<ControlTemplate x:Key="OptionsSelectorTemplate">
    <StackPanel Orientation="Vertical">
        <dxl:NavBarControl...>
            <dxn:NavBarControl.Groups>
                <dxn:NavBarGroup Header="Options Set 1">
                    <DataGrid Style="{StaticResource DataGridTemplateStyle}" ItemSource="{Binding ViewModel.Options}" DataContext="{Binding .}"/>
                </dxn:NavBarGroup>
                <dxn:NavBarGroup Header="Options Set 2">
                    <DataGrid Style="{StaticResource DataGridTemplateStyle}" ItemSource="{Binding ViewModel.Options}" DataContext="{Binding .}"/>
                </dxn:NavBarGroup>
        </dxl:NavBarControl...>
        <Button ... />
    </StackPanel>
</ControlTemplate>

<Style x:Key="DataGridTemplateStyle" TargetType="DataGrid">
    <Setter Property="Template" Value="{StaticResource DataGridTemplate}" />
</Style>     

<ControlTemplate x:Key="DataGridTemplate">
    <DockPanel HorizontalAlignment="Stretch">
        <DataGrid Name="_dataGrid" 
              AutoGenerateColumns="False" RowHeaderWidth="0" HeadersVisibility="Column" CanUserAddRows="False" GridLinesVisibility="None" HorizontalAlignment="Stretch" KeyboardNavigation.TabNavigation="Cycle">

            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Col1" MinWidth="75" Width="Auto" KeyboardNavigation.IsTabStop="False">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox.../>  <---Want this guy to size to always fit the selected list item and to expand the entire datagrid if need be w/o displaying horizontal scroll bar.
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Weight" MinWidth="40"  >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Border >
                                <TextBox T../>
                            </Border>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="NN" Width="30">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox .../>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

    </DockPanel>
</ControlTemplate>

1 个答案:

答案 0 :(得分:0)

根据文档看起来,DataGrid列仅在一个方向上调整大小...这只是它们变得更宽,而不是更窄。