14

First! I know it can seem a kind of dup nevertheless it isn't.

I have a grid as a data template for DataGrid.RowDetails. It has three columns: two with Width="Auto" and the 3d with Width="*"

<DataTemplate x:Key="NotEmptyDistributionsTemplateKey">
    <DataGrid ItemsSource="{Binding SoftwareVersionDistributions}"
        CanUserAddRows="False"
        CanUserDeleteRows="False"
        CanUserReorderColumns="False"
        CanUserResizeColumns="True"
        CanUserSortColumns="False"
        IsReadOnly="True"
        CanUserResizeRows="False"
        AutoGenerateColumns="False"
        RowHeaderWidth="0"
        Margin="20,5"
        HorizontalAlignment="Stretch">
        <DataGrid.Columns>
            <DataGridTextColumn 
                Header="Architecture" 
                Binding="{Binding SoftwareArchitecture.Name, TargetNullValue=Все, FallbackValue=Все}"
                Width="Auto"/>
            <DataGridTextColumn 
                Header="Language" 
                Binding="{Binding SysCodepage.Title, TargetNullValue=Все, FallbackValue=Все}" 
                Width="Auto"/>
            <DataGridHyperlinkColumn 
                Header="Link" 
                Binding="{Binding DownloadLink}"
                Width="*" 
                CanUserResize="False"/>
        </DataGrid.Columns>
    </DataGrid>
</DataTemplate>

This template shows exactly 3 columns, if Grid is nested in a cell. But when it is used as RowDetaislTemplate 4th empty column appears. This makes me mad.

Does anybody have ideas why this can happen?

Update 1. How it looks:

enter image description here

Update 2.

Finally I found the soultion. Just added ScrollViewer.HorizontalScrollBarVisibility="Disabled"

Now it's ok:

enter image description here

But I cannot understand this odd behavior.

Update 3 (may be helpful to someone)

Recently we've found a bug which our users had and the developers did not. The bug was again about DataGrid's width and alignment. Well, may be this was not really a bug, but we had different behavior of the control.
The research revealed that when installing .net framework 4.5 installer also updated .net framework 4.0. This update changed somehow DataGrid's behavior. (to the expected one)
We asked our users to install framework 4.5 and the problem dissapeared. Though our app still targets .net 4.0

Community
  • 1
  • 1
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
  • Can you post a screenshot, I suspect the problem is the parent grid giving a * width column – Charleh Aug 14 '12 at 16:02
  • @Charleh. You are right. The width of a column is *. But when this data template serves as a template for a cell everything works fine. RowDetailsTemplate is a problem. – Pavel Voronin Aug 14 '12 at 17:54
  • @Charleh. I'll post screenshot tomorrow – Pavel Voronin Aug 14 '12 at 18:09
  • @Charleh. It's getting more interesting=) I tried to reproduce the bug at home with simple markup and found that RowDetailsTemplate has unlimitied width. But in my real application it is somehow magically restricted and occupies exactly the width of ItemsPresenter. – Pavel Voronin Aug 14 '12 at 19:31
  • What happens if you put the DataGrid that is in the DataTemplate inside of a Grid, or a StackPanel? – William Jan 09 '13 at 00:24
  • Actually, I think I figured out what is going on. The issue is similar to when a DataGrid has HorizontalAlignment=Stretch and not enough columns. I would recomment either setting the width of the last column manually (which may not be practical) or binding each column's Width property to something in a ViewModel and then calculate the Width of the last column. Or, if the DG doesn't have to take up the entire width of its container, make HorizontalAlignment=Left. – William Jan 09 '13 at 02:05
  • 2
    I think it is better if you write the answer as an answer. I missed that you had found a solution. – Johan Larsson Jan 21 '13 at 19:37

1 Answers1

7

Finally I found the soultion.

Just added ScrollViewer.HorizontalScrollBarVisibility="Disabled"

For those who targets Framework v4.0
Recently we found a bug which our users had and the developers did not. The bug was again about DataGrid's width and alignment. Well, may be this was not really a bug but we had different behavior of the control. The research revealed that when installing .net framework 4.5 installer also updated .net framework 4.0. This update changed somehow DataGrid's behavior. (to the expected one) We asked our users to install framework 4.5 and the problem disappeared. Though our app still targets .net 4.0

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137