Problems resizing columns with DataGrid

Jun 15, 2009 at 8:24 PM

 Hello. I have problems resizing some columns with DataGrid. I customized my DataGrid with a ColumnHeader. I have the following problem: If I try to resize a column Cn when the column C(n+1) has Visibility = Collaped Cn doesn't rezise anymore. I hope you understand me. How can I resolve it? Thanks!

 

<Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
            <Setter Property="Width" Value="8"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Cursor" Value="SizeWE"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Thumb}">
                        <Border Padding="{TemplateBinding Padding}"
                    Background="{TemplateBinding Background}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
     <Style TargetType="{x:Type my:DataGridColumnHeader}">
            <Setter Property="VerticalContentAlignment" Value="Stretch" />
            <Setter Property="Foreground" Value="{DynamicResource LibraryListView_Foreground}" />
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Margin" Value="0,0,0,0"/>
            <Setter Property="MinHeight" Value="20" />
            <Setter Property="FontFamily" Value="Verdana"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type my:DataGridColumnHeader}">
                        <Grid ContextMenuOpening="Grid_ContextMenuOpening" Visibility="Visible" > 
                            <Grid.ContextMenu>
                                <ContextMenu StaysOpen="False" BorderBrush="#FF5A5A5A" Foreground="#FFFFFFFF" ItemContainerStyle="{DynamicResource MenuItemStyle2}" HasDropShadow="True" IsTextSearchEnabled="False" Background="#FF1D1D1D" FontFamily="Verdana">
                                    <CheckBox Content="{DynamicResource Library_ListView_ColumnName_Duration}" Style="{DynamicResource CheckBoxStyle1}" FontFamily="Verdana" FontSize="11" Foreground="#FFC8C8C8"/>
                                    <CheckBox Style="{DynamicResource CheckBoxStyle1}" Content="{DynamicResource Library_ListView_ColumnName_ArtistName}" FontFamily="Verdana" FontSize="11" Foreground="#FFC9C9C9"/>
                                    <CheckBox Style="{DynamicResource CheckBoxStyle1}" Content="{DynamicResource Library_ListView_ColumnName_AlbumName}" FontFamily="Verdana" FontSize="11" Foreground="#FFC9C9C9"/>
                                    <CheckBox Style="{DynamicResource CheckBoxStyle1}" Content="{DynamicResource Library_ListView_ColumnName_Rating}" FontFamily="Verdana" FontSize="11" Foreground="#FFC8C8C8"/>
                                    <CheckBox Style="{DynamicResource CheckBoxStyle1}" Content="{DynamicResource Library_ListView_ColumnName_Count}" FontFamily="Verdana" FontSize="11" Foreground="#FFC8C8C8"/>
                                    <CheckBox Style="{DynamicResource CheckBoxStyle1}" Content="{DynamicResource Library_ListView_ColumnName_Genre}" IsChecked="False" FontFamily="Verdana" FontSize="11" Foreground="#FFC8C8C8"/>
                                    <CheckBox Style="{DynamicResource CheckBoxStyle1}" Content="{DynamicResource Library_ListView_ColumnName_Year}" FontFamily="Verdana" FontSize="11" Foreground="#FFC8C8C8"/>
                                    <CheckBox Style="{DynamicResource CheckBoxStyle1}" Content="{DynamicResource Library_ListView_ColumnName_Bitrate}" FontFamily="Verdana" FontSize="11" Foreground="#FFC8C8C8"/>
                                    <CheckBox Style="{DynamicResource CheckBoxStyle1}" Content="{DynamicResource Library_ListView_ColumnName_Title}" FontFamily="Verdana" FontSize="11" Foreground="#FFC8C8C8"/>

                                </ContextMenu>
                            </Grid.ContextMenu>
                            <Border x:Name="MainBorder" CornerRadius="0,0,0,0" Margin="0,0,0,0" Background="Transparent">
                                <Grid>
                                    <!-- an overlayer glass effect -->
                                    <Border VerticalAlignment="Stretch" x:Name="GradientBorder" Height="Auto" CornerRadius="0,0,0,0"  BorderBrush="#D99466" />
                                    <ContentPresenter Margin="15,0,0,5" HorizontalAlignment="Left" VerticalAlignment="Center" Content="{TemplateBinding Content}"
                                                      ContentStringFormat="{TemplateBinding ContentStringFormat}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                                </Grid>
                            </Border>
                            <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" 
                               Style="{StaticResource ColumnHeaderGripperStyle}"/>
                            <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" 
                               Style="{StaticResource ColumnHeaderGripperStyle}"/>
                            <Canvas x:Name="canvas" Visibility="Visible">
                                <Path x:Name="SortArrow" Visibility="Collapsed" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill" 
                              Grid.Column="1" Width="8" Height="6" Fill="Gray" Canvas.Right="2" Canvas.Top="6"
                              VerticalAlignment="Center" RenderTransformOrigin="0.5,0.4"  />

                                <Thumb x:Name="PART_HeaderGripper" Background="#FFEAEAEA" Margin="0,0,0,0" Padding="0,3,3,4">
                                <Thumb.Style>
                                    <Style TargetType="{x:Type Thumb}">
                                        <Setter Property="Width" Value="18"/>
                                        <Setter Property="Height" Value="{Binding Path=ActualHeight, RelativeSource={RelativeSource TemplatedParent}}"/>
                                        <Setter Property="Padding" Value="0,3,3,4"/>
                                        <Setter Property="Background" Value="#FFC7C5B2"/>
                                            <Setter Property="Canvas.Right" Value="-5"/>
                                            <Setter Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="{x:Type Thumb}">
                                                    <Border Background="#00FFFFFF" Padding="{TemplateBinding Padding}">
                                                        <DockPanel HorizontalAlignment="Right">
                                                            <Rectangle Width="1" Fill="#3FC8C8C8"/>
                                                            <Rectangle Width="1" Fill="#3FC8C8C8"/>
                                                        </DockPanel>
                                                    </Border>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </Thumb.Style>
                            </Thumb>
                                </Canvas>
                        </Grid>
                        <ControlTemplate.Triggers>
                    		<Trigger Property="SortDirection" Value="Descending">
                    			<Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                    		</Trigger>
                    		<Trigger Property="SortDirection" Value="Ascending">
                    			<Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                    			<Setter TargetName="SortArrow" Property="RenderTransform">
                    				<Setter.Value>
                    					<RotateTransform Angle="180" />
                    				</Setter.Value>
                    			</Setter>
                    		</Trigger>
                    		<Trigger Property="DisplayIndex" Value="0">
                    			<Setter Property="Visibility" TargetName="PART_LeftHeaderGripper" Value="Collapsed"/>
                    		</Trigger>
                    	</ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>