无法降低DataGridCell的高度以适合内容并删除WPF中的任何空格

时间:2019-05-09 13:30:14

标签: c# wpf xaml

我下面有DataGrid

 <DataGrid x:Name="DisplayMappingGrid"  ColumnHeaderHeight="25" Background="White" HorizontalAlignment="Stretch" ColumnWidth="*"  VerticalAlignment="Stretch" Grid.Row ="4"  HeadersVisibility="Column"   AutoGeneratingColumn="DisplayGrid_AutoGeneratingColumn"  IsReadOnly="True" Visibility="Collapsed" BorderBrush="Black"  BorderThickness="1" Margin="0,10,0,10"  AlternationCount="2" AlternatingRowBackground="#E8EEEE">

                        <DataGrid.Resources>

                            <Style TargetType="{x:Type DataGridCell}">

                            <EventSetter  Event="GotFocus" Handler="DisplayMappingGrid_GotFocus"/>
                            <Style.Triggers>
                                <Trigger Property="DataGridCell.IsSelected" Value="True">
                                    <Setter Property="Background" Value="#98D4F5" />
                                    <Setter Property="Foreground" Value="Black"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                            <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Setter Property="FontWeight"
                  Value="Bold" />
                            <Setter Property="HorizontalContentAlignment"
                 Value="Center" />
                            <Setter Property="Background" Value="FloralWhite"/>

                            <Setter Property="FontWeight" Value="Bold" />
                        </Style>


                        </DataGrid.Resources>


                    </DataGrid >

这是DataGrid

使用的样式
 <Style TargetType="DataGrid" >

            <Setter Property="BorderBrush" Value="#DDDDDD" />
            <Setter Property="HorizontalGridLinesBrush" Value="#DDDDDD" />
            <Setter Property="VerticalGridLinesBrush" Value="#DDDDDD" />

            <Setter Property="RowStyle">
                <Setter.Value>
                    <Style TargetType="DataGridRow">
                        <Style.Triggers>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                                <Setter Property="Background" Value="White" />
                            </Trigger>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                <Setter Property="Background" Value="#E8EEEE" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="LightCyan" />
                                <Setter Property="Foreground" Value="Black"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Foreground" Value="Black"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="CellStyle">
                <Setter.Value>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>

                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="#ff66ccff" />
                                <Setter Property="BorderBrush" Value="Transparent" />
                                <Setter Property="Foreground" Value="Black" />
                            </Trigger>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="Background" Value="#ff66ccff" />
                                <Setter Property="BorderBrush" Value="Black" />
                                <Setter Property="BorderThickness" Value="1"/>
                                <Setter Property="Foreground" Value="Black" />
                            </Trigger>
                        </Style.Triggers>


                        <Setter Property="BorderBrush" Value="Transparent"/>
                        <Setter Property="BorderThickness" Value="1"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="DataGridCell">
                                    <Border Padding="{TemplateBinding Padding}" 
                                            BorderBrush="{TemplateBinding BorderBrush}"
                                            BorderThickness="{TemplateBinding BorderThickness}"
                                            Background="{TemplateBinding Background}">
                                        <ContentPresenter />
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="FocusVisualStyle">
                            <Setter.Value>
                                <Style TargetType="Control">
                                    <Setter Property="BorderBrush" Value="Transparent" />
                                </Style>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>

我的问题是,一旦填充了数据,我就会在每个单元格的底部看到空格。 我在最后一栏中有2张图片,removing并没有帮助我删除单元格空间。检查下面的图像。 enter image description here

Auto-Generating Column活动在这里

 private void DisplayGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {

            e.Column.HeaderStyle = new Style(typeof(DataGridColumnHeader));
            e.Column.HeaderStyle.Setters.Add(new Setter(HorizontalContentAlignmentProperty, HorizontalAlignment.Center));
            e.Column.HeaderStyle.Setters.Add(new Setter(FontWeightProperty, FontWeights.Bold));
            // e.Column.HeaderStyle.Setters.Add(new Setter(BackgroundProperty, Colors.Red));
            e.Column.HeaderStyle.Setters.Add(new Setter(BackgroundProperty, Brushes.FloralWhite));
            // e.Column.HeaderStyle.Setters.Add(new Setter(MarginProperty, new Thickness(5)));
            const string Xaml = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><ContentControl Content=\"{{Binding {0}}}\" /></DataTemplate>";


            if (e.Column.Header.ToString() == "CMT")
            {
                var col = e.Column as DataGridTextColumn;

                var style = new Style(typeof(TextBlock));
                style.Setters.Add(new Setter(TextBlock.TextWrappingProperty, TextWrapping.Wrap));
                //style.Setters.Add(new Setter(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center));

                col.ElementStyle = style;
            }


            if (e.PropertyType == typeof(Image))
            {
                DataGridTemplateColumn tc = new DataGridTemplateColumn();
                tc.Header = e.Column.Header.ToString();
                tc.CellTemplate = System.Windows.Markup.XamlReader.Parse(string.Format(Xaml, e.PropertyName)) as DataTemplate;

                // tc.HeaderStyle.Setters.Add(new Setter(HorizontalContentAlignmentProperty, HorizontalAlignment.Center));
                //tc.HeaderStyle.Setters.Add(new Setter(FontWeightProperty, FontWeights.Bold));

                // tc is created object
                e.Column = tc;
            }

            if (e.PropertyType == typeof(Button))
            {
                DataGridTemplateColumn tc = new DataGridTemplateColumn();
                tc.CellTemplate = System.Windows.Markup.XamlReader.Parse(string.Format(Xaml, e.PropertyName)) as DataTemplate;
                e.Column = tc;
            }

            if ((e.PropertyType == typeof(Int32) || e.PropertyType == typeof(Decimal)) && e.Column.Header.ToString() == identityColName)
            {
                DataGridTemplateColumn tc = new DataGridTemplateColumn();
                tc.CellTemplate = System.Windows.Markup.XamlReader.Parse(string.Format(Xaml, e.PropertyName)) as DataTemplate;

                e.Column.Visibility = Visibility.Hidden;
            }

            if (e.PropertyType == typeof(Nullable))
            {
                DataGridTemplateColumn tc = new DataGridTemplateColumn();
                tc.CellTemplate = System.Windows.Markup.XamlReader.Parse(string.Format(Xaml, e.PropertyName)) as DataTemplate;

                e.Column.Visibility = Visibility.Hidden;
            }



        }

0 个答案:

没有答案