所以我已经使用wpf 3天了,我无法弄清楚为什么数据表值没有显示在数据网格中,Windows窗体中的datagridview不是这种情况,我从10天开始就使用了它
我尝试过编程和 xaml 绑定,但它们都不适合我
首先,我创建了数据网格作为单独的用户控件,并将文件附加到主窗口,但无法看到数据,所以我想如果数据显示在 mainwinow 本身中,也许我应该进行测试,然后我将数据网格分成它自己的用户控件,但它们都不起作用
这是MainWindow.xaml
<Border>
<Grid>
<Border Grid.Row="1" CornerRadius="20" Margin="15,0,15,15" >
<!-- Apply Style with Rounded Corners to DataGrid -->
<DataGrid
IsReadOnly="True"
x:Name="dataGrid"
AutoGenerateColumns="False">
<DataGrid.Resources>
<!-- Cell Background -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#121212"/>
<!-- Cell Foreground (Text) -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="#FFFFFF"/>
<!-- Grid Lines -->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveBorderBrushKey}" Color="#1E1E1E"/>
<!-- Header Background -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkDarkBrushKey}" Color="#2E2E2E"/>
<!-- Header Foreground (Text) -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlLightLightBrushKey}" Color="#FFFFFF"/>
</DataGrid.Resources>
<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid}">
<Border Background="{TemplateBinding Background}" CornerRadius="20">
<ScrollViewer>
<!-- ContentPresenter displays the actual DataGrid content -->
<ContentPresenter/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Style>
</DataGrid>
</Border>
</Grid>
</Border>
这是MainWindow.xaml.cs
public partial class MainWindow : Window
{
private DataTable dataTable;
public MainWindow()
{
InitializeComponent();
InitializeDataGridAsync();
}
private async void InitializeDataGridAsync()
{
var newDT = CreateDataTable();
// Simulate a delay for loading data (replace this with your actual data loading logic)
await Task.Delay(3000);
// Set the DataTable as the DataGrid's ItemsSource
dataGrid.ItemsSource = newDT.DefaultView;
}
private DataTable CreateDataTable()
{
// Create a DataTable with columns
dataTable = new DataTable("MyDataTable");
// Add 10 sample columns
for (int i = 1; i <= 100; i++)
{
dataTable.Columns.Add($"Column{i}", typeof(int));
}
// Add 100 rows of sample data
for (int row = 1; row <= 100; row++)
{
// Create an array to hold values for each column
object[] values = new object[100];
// Assign sample values to each column
for (int col = 0; col < 100; col++)
{
values[col] = row * 100 + col + 1; // You can change this to your desired data
}
// Add the row to the DataTable
dataTable.Rows.Add(values);
}
return dataTable;
}
}
这是 refrecne 的屏幕截图,第一个灰卡是数据网格所在的位置