我是WPF的新手,我正在开发一个项目,我正在使用WPF和DataGrid来显示DataTable。
我有列和行标题可见,我遇到的问题是,在调用WPF窗口的方法完成之前,DataGrid左上角的SelectAll按钮不会出现。这会导致列标题未对齐,直到出现SelectAll按钮。
我在一个非常简单的项目中重新创建了这个问题。
我可以在设计模式中看到SelectAll按钮,结果仍然相同。有人可以帮忙吗?下面的图片显示了我在说什么。
我的XAML在其后面是创建DataTable
的C#<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="WpfApplication1.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450px" Width="800px">
<Window.Resources>
<Style TargetType="{x:Type Button}"
x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
Storyboard.TargetName="Arrow">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{x:Static Visibility.Collapsed}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="Border"
SnapsToDevicePixels="True">
<Rectangle.Stroke>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="{DynamicResource BorderLightColor}"
Offset="0" />
<GradientStop Color="{DynamicResource BorderMediumColor}"
Offset="1" />
</LinearGradientBrush>
</Rectangle.Stroke>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="{DynamicResource BorderLightColor}"
Offset="0" />
<GradientStop Color="{DynamicResource BorderMediumColor}"
Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Polygon x:Name="Arrow"
HorizontalAlignment="Right"
Margin="8,8,3,3"
Opacity="0.55"
Points="0,10 10,10 10,0"
Stretch="Uniform"
VerticalAlignment="Bottom">
<Polygon.Fill>
<SolidColorBrush Color="White" />
</Polygon.Fill>
</Polygon>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="73*"/>
<ColumnDefinition Width="502*"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="120"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Button x:Name="BtnImport" Content="Import" Grid.Row="2" Grid.Column="3" Height="20" Width="100">
<Button.ToolTip>
<ToolTip>
<StackPanel>
<TextBlock FontWeight="BOLD"><Run Text="Import to AutoCAD"/></TextBlock>
<TextBlock><Run Text="Click or press ALT+ENTER"/></TextBlock>
</StackPanel>
</ToolTip>
</Button.ToolTip>
</Button>
<Button x:Name="BtnCancel" Content="Cancel" Grid.Row="2" Grid.Column="2" Height="20" Width="100">
<Button.ToolTip>
<ToolTip>
<StackPanel>
<TextBlock FontWeight="BOLD"><Run Text="Cancel"/></TextBlock>
<TextBlock><Run Text="Click or press ESCAPE"/></TextBlock>
</StackPanel>
</ToolTip>
</Button.ToolTip>
</Button>
<StatusBar Grid.Row="3" Grid.ColumnSpan="4" Grid.Column="0">
<StatusBarItem >
<TextBlock x:Name="AppsVer"><Run Text="AppsVer"/></TextBlock>
</StatusBarItem>
</StatusBar>
<DataGrid x:Name="ExportGrid" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" ColumnHeaderHeight="50" HeadersVisibility="All"
AutoGenerateColumns="True" RowHeaderWidth="40"/>
</Grid>
using System.Data;
namespace WpfApplication1
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
ExportGrid.ItemsSource = MakeTable().DefaultView;
}
public static DataTable MakeTable()
{
var tbl = new DataTable();
for (int i = 1; i < 5; i++)
{
tbl.Columns.Add("Column" + i, typeof(int));
}
for (int j = 0; j <= 30; j++)
{
var nuRow = tbl.NewRow();
for (int k = 0; k < 4; k++)
{
nuRow[k] = j;
}
tbl.Rows.Add(nuRow);
}
return tbl;
}
}
}
答案 0 :(得分:0)
我无法以您描述的方式重现您的问题。
但是根据你的描述,它可能是一个用异步方法填充网格的解决方案。在这种情况下,视图将不会阻塞,直到方法完成并且网格应正确绘制。
我添加了一个代码片段,它将异步填充网格:
{{1}}