1

我的应用程序中有一个DataGrid控件:

<DataGrid AutoGenerateColumns="False"
          BorderThickness="1"
          CanUserDeleteRows="False"
          CanUserReorderColumns="False"
          CanUserResizeColumns="True"
          CanUserResizeRows="False"
          CanUserSortColumns="True"
          EnableColumnVirtualization="True"
          EnableRowVirtualization="True"
          FontSize="16"
          FontWeight="Bold"
          IsReadOnly="True"
          Grid.Column="0"
          Grid.ColumnSpan="2"
          Grid.Row="2"
          Margin="5"
          Name="PendingRowsGrid"
          SelectionUnit="FullRow"
          ScrollViewer.CanContentScroll="True"
          ScrollViewer.HorizontalScrollBarVisibility="Auto"
          ScrollViewer.VerticalScrollBarVisibility="Auto"
          ToolTip="Number of rows pending transmission"
          Visibility="{Binding Converter={StaticResource BoolToHide}, Path=AdvancedSettings.RunningStandAlone, RelativeSource={RelativeSource AncestorType={x:Type cs:Dashboard}}}">
    <DataGrid.Columns>
        <cs:ExtendedTextColumn Binding="{Binding Mode=OneWay, Path=Value.DataTypeDisplay}"
                               Header="Data Type"
                               Width="*" />
        <cs:ExtendedTextColumn Binding="{Binding Converter={StaticResource CountConverter}, ConverterParameter='#,##0', Mode=OneWay, Path=Value.ToEoc}"
                               Header="To EOC"
                               HorizontalAlignment="Right"
                               Width="Auto" />
        <cs:ExtendedTextColumn Binding="{Binding Converter={StaticResource CountConverter}, ConverterParameter='#,##0', Mode=OneWay, Path=Value.FromEoc}"
                               Header="From EOC"
                               HorizontalAlignment="Right"
                               Width="Auto" />
    </DataGrid.Columns
</DataGrid>

第一列左对齐,最后两列需要右对齐。此外,最右边的 2 列需要调整它们的宽度以适应列中最宽的值。

这是ExtendedTextColumn该类的代码:

public class ExtendedTextColumn : DataGridTextColumn {

    public static readonly DependencyProperty HorizontalAlignmentProperty =
        DependencyProperty.Register( "HorizontalAlignment", typeof( HorizontalAlignment ), typeof( ExtendedTextColumn ),
                                     new PropertyMetadata( HorizontalAlignment.Stretch ) );

    public static readonly DependencyProperty VerticalAlignmentProperty =
        DependencyProperty.Register( "VerticalAlignment", typeof( VerticalAlignment ), typeof( ExtendedTextColumn ),
                                     new PropertyMetadata( VerticalAlignment.Stretch ) );

    public HorizontalAlignment HorizontalAlignment {
        get { return (HorizontalAlignment) GetValue( HorizontalAlignmentProperty ); }
        set { SetValue( HorizontalAlignmentProperty, value ); }
    }

    public VerticalAlignment VerticalAlignment {
        get { return (VerticalAlignment) GetValue( VerticalAlignmentProperty ); }
        set { SetValue( VerticalAlignmentProperty, value ); }
    }

    protected override FrameworkElement GenerateElement( DataGridCell cell, object dataItem ) {
        FrameworkElement element = base.GenerateElement( cell, dataItem );

        // Set the FrameworkElement's HorizontalAlignment and VeritcalAligment properties
        element.HorizontalAlignment = HorizontalAlignment;
        element.VerticalAlignment   = VerticalAlignment;
        return element;
    }

    protected override FrameworkElement GenerateEditingElement( DataGridCell cell, object dataItem ) {
        TextBox textBox = (TextBox) base.GenerateEditingElement( cell, dataItem );

        // Set the TextBox's TextAlignment and VeritcalAligment properties
        textBox.TextAlignment            = GetTextAlignment();
        textBox.VerticalContentAlignment = VerticalAlignment;
        return textBox;
    }

    private TextAlignment GetTextAlignment() {
        switch ( HorizontalAlignment ) {
            case HorizontalAlignment.Center:
                return TextAlignment.Center;

            case HorizontalAlignment.Left:
                return TextAlignment.Left;

            case HorizontalAlignment.Right:
                return TextAlignment.Right;

            default:
                return TextAlignment.Justify;
        }
    }
}

}

问题是最右边的两列的大小没有正确显示 XAML。我也尝试使用DataGridTemplateColumns最右边的两列:

        <DataGridTemplateColumn Header="To EOC"
                                HeaderStyle="{StaticResource CenteredHeaderText}"
                                Width="Auto">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock TextAlignment="Right" 
                               Text="{Binding Converter={StaticResource CountConverter}, ConverterParameter='#,##0', Mode=OneWay, Path=Value.ToEoc}" 
                               VerticalAlignment="Center" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="From EOC"
                                HeaderStyle="{StaticResource CenteredHeaderText}"
                                Width="Auto">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock TextAlignment="Right" 
                               Text="{Binding Converter={StaticResource CountConverter}, ConverterParameter='#,##0', Mode=OneWay, Path=Value.FromEoc}" 
                               VerticalAlignment="Center" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

但是这些也没有适当的大小。在这两种情况下,一到一个半的字符都会被截断。在原始 XAML(使用ExtendedTextColumn类)的情况下,最左边的字符被截断;在第二个 XAML(DataGridTemplateColumn案例)中,它是最右边的字符。

我尝试更改原始 XAML,以便最右边两列的DataGrid用途和这些列的大小正确。DataGridTextColumns这使我相信在其他两种情况下没有完成DataGridorDataGridTextColumn类所做的某些事情来确定列的宽度,但我的ExtendedTextColumn类从DataGridTextColumn. 我不知所措。

有没有人对我如何让它正常工作有任何建议?

4

1 回答 1

1

在玩了一天大部分时间的 Xaml 和代码之后,我想出了一些几乎可以工作的东西。

首先,我停止使用我的类,而是对右侧的两列使用类似于以下的 XAML:

<DataGridTextColumn Binding="{Binding Value.FromEoc, ConverterParameter=#\,##0, Converter={StaticResource CountConverter}, Mode=OneWay}"
                    Header="From EOC"
                    HeaderStyle="{StaticResource CenteredHeaderText}"
                    MinWidth="80"
                    Width="*">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="HorizontalAlignment" Value="Right" />
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

我认为,由于DataGridTextColumn它本身已正确调整大小,因此可以解决问题。唉,它没有。列最终太窄了 1/2 个字符!

所以最后,我踢了。最初的问题是,如果要显示的位数为 8 或更大,则列太窄。我将Width第一列的更改为AutoWidths将最后两列的 更改为*。列现在足够宽,在位数大于 12 左右之前我不应该有问题,而且我们在生产系统中不应该有那么多位数。

于 2013-07-08T20:20:47.863 回答