我是WPF的新手,我正在使用WPF和DataGrid顯示DataTable的項目。WPF DataGrid SelectAll Button出現一次DataGrid加載
我有列和行標題可見,我的問題是,直到調用WPF窗口的方法完成後,DataGrid左上方的SelectAll按鈕纔會出現。這會導致列標題未對齊,直到出現SelectAll按鈕。
我已經在一個非常簡單的項目中重新創建了該問題。
我可以在設計模式下看到SelectAll按鈕,結果仍然相同。任何人都可以幫忙嗎?下面的圖片展示了我正在談論的內容。
我的XAML如下其次它背後的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;
}
}
}
我不能和你的執行重現該問題?窗口出現後,網格就會正確渲染。你能提供進一步的解釋或代碼問題是可見的嗎? –
嗨Eike B感謝您的評論。你編譯這個並從.exe運行?因爲我同意從.exe它渲染罰款。如果您從Visual Studio中運行它,那麼當您看到我描述的症狀時。 –
我正在使用的項目是AutoCAD的插件,即使我構建發佈版本並在AutoCAD內部運行,而不是從Visual Studio中進行調試,但我仍然在列標題中出現了移位。 –