0

I have several tabitems, each of them have a DataGrid in it, and I set DataGrid's attribute "AutoGenerateColumns" to false. The TabControl is embraced by a ScrollViewer, Here is the question, every DataGrid in every TabItem has an empty column in the end. I googled around, but get few things useful. Here's the code structure.

<ScrollViewer>
    <TabControl>
        <TabItem>
            <DataGrid AutoGenerateColumns="False">
            </DataGrid>
        </TabItem>
    </TabControl>
</ScrollViewer> 

Can anyone help me ? Thanks a lot.

Update

OK, actually the situation is, I have several TabItems in a TabControl, but the width of TabControl is limited, so I gonna use ScrollViewer to "scroll" TabItems, so I can see these TabItems clearly, and that's the reason why it results in the current problem. I think, the ScrollViewer effectes the TabItems total width, and indirectly, effectes the width of DataGrid in each TabItem, so the sum width of columns in DataGrid less than the width of DataGrid, therefore, there's an empty column in the right side.

I try to add another ScrollViewer around DataGrid, but failed, there's still a blank part in the right side.

user1108069
  • 503
  • 2
  • 5
  • 16
  • If the datagrid's width is greater than the width of the columns, then there is empty space left at the end. It is actually not a column but row surface with no cells. If this is the case, you would not be able to click in that area to select a row. Is that the case? – Eren Ersönmez May 08 '12 at 10:21
  • @ErenErsönmez yes, it is. What can I do to remove it ? – user1108069 May 08 '12 at 10:24
  • You basically need to ensure the columns fill the datagrid. For example, set width = auto and horizontalalignment=center. – Eren Ersönmez May 08 '12 at 10:28
  • I tried, but failed, the empty column is gone, but there's still a blank part in the right side in DataGrid, just like what you said, the datagrid's width > the width of the columns, please see the update of the question. – user1108069 May 08 '12 at 11:15

1 Answers1

1

As @ErenErsönmez answered the behaviour is the expected behaviour from the DataGrid. To alter it you need to set the widths of your columns with at least one of them set to span. I have used the following to make the rows span the width of the datagrid.

<TabControl>
    <TabItem Header="Tab Item 1">
        <DataGrid AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Column A" Width="Auto" />
                <!-- The following column grows to span the available area -->
                <DataGridTextColumn Header="Column B" Width="*" MinWidth="100" />
                <DataGridTextColumn Header="Column C" Width="Auto" />
            </DataGrid.Columns>
        </DataGrid>
    </TabItem>
<TabControl>

In terms of scrolling the TabControl itself, could you post some details as to what you are trying to do. I believe, that you're trying to the entire TabControl scroll due to have a large number of tabs. Perhaps something like this (not tested) would help http://rickdoes.net/post/2009/11/06/WPF-Single-Row-Tab-Control.aspx

bebleo
  • 326
  • 4
  • 12
  • I don't believe that you will need the ScrollViewer at all as the datagrid integrates a scrollviewer and displays ScrollBars as needed. I will update my answer based on your comments. – bebleo May 08 '12 at 11:24
  • Hi James, as I updated, I don't wanna scroll the content in **DataGrid**, actually I wanna scroll TabItems in a TabControl, and the ScrollView affecting TabItems also affects the DataGrid in TabItems, that's why this happened. – user1108069 May 08 '12 at 11:33
  • and I tried you solution , the column whose width is set to * is so wide, so this doesn't suit my situation, anyway, thank you~ – user1108069 May 08 '12 at 11:34
  • Your answer doesn't work, but the link you provide is nice, I solved my problem by the knowledge in that page. :) – user1108069 May 10 '12 at 07:48