Tutorial :ItemsSource data type displayed in DataGrid



Question:

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


Solution:1

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »