我的应用程序中有一个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
这使我相信在其他两种情况下没有完成DataGrid
orDataGridTextColumn
类所做的某些事情来确定列的宽度,但我的ExtendedTextColumn
类从DataGridTextColumn
. 我不知所措。
有没有人对我如何让它正常工作有任何建议?