tags:

views:

17

answers:

1

Hello Stackies

I'm using a DataGrid inside a StackPanel inside a Grid which causes the GridView to be wider than its preferred size. All the columns are displayed correctly but after the last column the end of the ItemsSource data type name is displayed. Apparently its is drawn in the background and hidden by the columns in the normal case.

I tried to add an empty column with MinWidth="0" and Width="*" but like that the horizontal scrollbar is not displayed if necessary.

How do I hide the data type text?

XAML code:

    <Grid Margin="5,5,5,5"
      Grid.IsSharedSizeScope="True">
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="30" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Row="0"
                Grid.Column="0"
                Orientation="Vertical">
        <DataGrid ItemsSource="{Binding BudgetPositions}"
                  IsSynchronizedWithCurrentItem="True"
                  AutoGenerateColumns="False"
                  GridLinesVisibility="None"
                  HeadersVisibility="Column"
                  CanUserAddRows="False"
                  CanUserDeleteRows="False">
            <DataGrid.Resources>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <!-- Budget group column -->
                <DataGridTemplateColumn Header="Budget Group"
                                        Width="Auto">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock DataContext="{Binding GroupName}"
                                       Text="{Binding Value}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <!-- Budget position column -->
                <DataGridTemplateColumn Header="Budget Position"
                                        Width="Auto">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock DataContext="{Binding PositionName}"
                                       Text="{Binding Value}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Add budget position"
                x:Name="addPositionButton"
                Width="140"
                Command="{Binding BudgetPositions.AddNewCommand}" />
    </StackPanel>
    ...

Here's a screenshot of the DataGrid: The rightmost column shows the ItemsSource type name

Cheers AC

A: 

The XAML you posted won't display any type name. My best guess is that you have another control in row and column 0 of the Grid that is rendering behind the StackPanel, and that the content is an object that does not override ToString, so it displays the type name.

In case it helps: if you don't want the GridView to be wider than its preferred size, you can set the HorizontalAlignment property on either the StackPanel or the DataGrid to Left, Right, or Center. (Also, if the data type name is part of the StackPanel, then setting this on the StackPanel should cause it to be hidden again. If it is not part of the StackPanel, then it won't move if you set the StackPanel to align Right or Center, which may make the renegade element easier to find.)

Quartermeister
It's not my XAML which displays the typename it's the DataGrid. The text is not behind the DataGrid but behind the columns of the DataGrid. The style of the DataGrid is applied to the type name text.
Anonymous Coward
@Anonymous Coward: Do you have a custom template for the DataGrid or any custom styles? What theme are you running? If I run the XAML you posted in a new WPF project and add just enough of a data context to get the bindings working, I don't see any type name displayed. Do you get that behavior on an otherwise empty project?
Quartermeister
You're right of course. I forgot to remove my "fix". Sorry.
Anonymous Coward
@Anonymous Coward: I had tried that, but I still didn't see the text. I have a gray header, though, and your screenshot shows a blue one, so I must be using a different theme.
Quartermeister
@Anonymous Coward: Do you have any styles that could affect System.Windows.Controls.Primitives.DataGridColumnHeader? It looks like the header is rendered by having one DataGridColumnHeader stretch across the entire control in the background and then one for each column in the foreground. The background header should have no content, but you might be overriding it with a style.
Quartermeister