ItemsSource data type displayed in DataGrid
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 http://www.freeimagehosting.net/uploads/04e173120d.png
Cheers AC
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.)
精彩评论