动态表头
This commit is contained in:
@@ -161,17 +161,10 @@
|
|||||||
|
|
||||||
<!-- 表格列(你要的 6 列) -->
|
<!-- 表格列(你要的 6 列) -->
|
||||||
<DataGrid.Columns>
|
<DataGrid.Columns>
|
||||||
<DataGridTextColumn Header="编号" Binding="{Binding Id}" Width="80"/>
|
<DataGridTextColumn Header="编号" Binding="{Binding Id}" Width="60"/>
|
||||||
<DataGridTextColumn Header="时间" Binding="{Binding Time}" Width="120"/>
|
<DataGridTextColumn Header="时间" Binding="{Binding Time}" Width="120"/>
|
||||||
<DataGridTextColumn Header="日期" Binding="{Binding Date}" Width="120"/>
|
<DataGridTextColumn Header="日期" Binding="{Binding Date}" Width="120"/>
|
||||||
<DataGridTextColumn Header="ch.1" Binding="{Binding ChOne}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.2" Binding="{Binding ChTwo}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.3" Binding="{Binding ChThree}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.4" Binding="{Binding ChFour}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.5" Binding="{Binding ChFive}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.6" Binding="{Binding ChSix}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.7" Binding="{Binding ChSeven}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.8" Binding="{Binding ChEight}" Width="*"/>
|
|
||||||
</DataGrid.Columns>
|
</DataGrid.Columns>
|
||||||
</DataGrid>
|
</DataGrid>
|
||||||
|
|
||||||
@@ -205,14 +198,7 @@ ScrollViewer.HorizontalScrollBarVisibility="Auto"
|
|||||||
<DataGridTextColumn Header="编号" Binding="{Binding Id}" Width="80"/>
|
<DataGridTextColumn Header="编号" Binding="{Binding Id}" Width="80"/>
|
||||||
<DataGridTextColumn Header="时间" Binding="{Binding Time}" Width="120"/>
|
<DataGridTextColumn Header="时间" Binding="{Binding Time}" Width="120"/>
|
||||||
<DataGridTextColumn Header="日期" Binding="{Binding Date}" Width="120"/>
|
<DataGridTextColumn Header="日期" Binding="{Binding Date}" Width="120"/>
|
||||||
<DataGridTextColumn Header="ch.1" Binding="{Binding ChOne}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.2" Binding="{Binding ChTwo}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.3" Binding="{Binding ChThree}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.4" Binding="{Binding ChFour}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.5" Binding="{Binding ChFive}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.6" Binding="{Binding ChSic}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.7" Binding="{Binding ChSeven}" Width="*"/>
|
|
||||||
<DataGridTextColumn Header="ch.8" Binding="{Binding ChEight}" Width="*"/>
|
|
||||||
</DataGrid.Columns>
|
</DataGrid.Columns>
|
||||||
</DataGrid>
|
</DataGrid>
|
||||||
|
|
||||||
|
|||||||
@@ -18,63 +18,77 @@ namespace 头罩视野.Views
|
|||||||
/// RecordDate.xaml 的交互逻辑
|
/// RecordDate.xaml 的交互逻辑
|
||||||
/// </summary>
|
/// </summary>
|
||||||
///
|
///
|
||||||
public class VisionData
|
|
||||||
{
|
|
||||||
public int Id { get; set; }
|
|
||||||
public string Time { get; set; } = string.Empty;
|
|
||||||
public string Date { get; set; } = string.Empty;
|
|
||||||
public double ChOne { get; set; }
|
|
||||||
public double ChTwo { get; set; }
|
|
||||||
public double ChThree { get; set; }
|
|
||||||
public double ChFour { get; set; }
|
|
||||||
public double ChFive { get; set; }
|
|
||||||
public double ChSix { get; set; }
|
|
||||||
public double ChSeven { get; set; }
|
|
||||||
public double ChEight { get; set; }
|
|
||||||
}
|
|
||||||
public partial class RecordDate : Page
|
public partial class RecordDate : Page
|
||||||
|
|
||||||
{
|
{
|
||||||
public RecordDate()
|
public RecordDate()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
CreateTableData(); // 生成表格
|
// 1. 固定列:编号、时间、日期
|
||||||
|
//dataGrid1.Columns.Add(new DataGridTextColumn { Header = "编号", Binding = new System.Windows.Data.Binding("Num") });
|
||||||
|
//dataGrid1.Columns.Add(new DataGridTextColumn { Header = "时间", Binding = new System.Windows.Data.Binding("Time") });
|
||||||
|
//dataGrid1.Columns.Add(new DataGridTextColumn { Header = "日期", Binding = new System.Windows.Data.Binding("Date") });
|
||||||
|
DynamicHeader();
|
||||||
|
AddTestRow();
|
||||||
}
|
}
|
||||||
|
// 测试数据
|
||||||
// 核心:生成表格数据
|
void AddTestRow()
|
||||||
private void CreateTableData()
|
|
||||||
{
|
{
|
||||||
// 新建列表
|
var row = new Dictionary<string, object>
|
||||||
List<VisionData> list = new List<VisionData>();
|
{
|
||||||
|
{ "Num", 1 },
|
||||||
|
{ "Time", DateTime.Now.ToString("HH:mm:ss") },
|
||||||
|
{ "Date", DateTime.Now.ToString("yyyy-MM-dd") }
|
||||||
|
};
|
||||||
|
|
||||||
// 添加测试数据(可循环添加多条)
|
for (int i = 1; i <= 64; i++)
|
||||||
list.Add(new VisionData
|
row[$"Ch{i}"] = new Random().Next(10, 100);
|
||||||
{
|
|
||||||
Id = 1,
|
|
||||||
Time = "10:25:30",
|
|
||||||
Date = "2025-12-25",
|
|
||||||
ChOne = 66,
|
|
||||||
ChTwo = 12,
|
|
||||||
ChThree = 67,
|
|
||||||
ChFour = 89,
|
|
||||||
ChFive = 94,
|
|
||||||
ChSix = 87,
|
|
||||||
ChSeven = 21,
|
|
||||||
ChEight = 55,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
dataGrid1.Items.Add(row);
|
||||||
//System.Diagnostics.Debug.WriteLine("页面加载了!112222222");
|
dataGrid2.Items.Add(row);
|
||||||
|
|
||||||
// ####################################
|
|
||||||
// 绑定到 DataGrid 显示
|
|
||||||
dataGrid1.ItemsSource = list;
|
|
||||||
}
|
}
|
||||||
|
//动态生成表头
|
||||||
|
void DynamicHeader()
|
||||||
|
{
|
||||||
|
// 2. 循环生成 64 个 ch 列
|
||||||
|
for (int i = 1; i <= 64; i++)
|
||||||
|
{
|
||||||
|
dataGrid1.Columns.Add(new DataGridTextColumn
|
||||||
|
{
|
||||||
|
Header = $"ch.{i}",
|
||||||
|
Binding = new System.Windows.Data.Binding($"Ch{i}")
|
||||||
|
});
|
||||||
|
dataGrid2.Columns.Add(new DataGridTextColumn
|
||||||
|
{
|
||||||
|
Header = $"ch.{i}",
|
||||||
|
Binding = new System.Windows.Data.Binding($"Ch{i}")
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void btnBack_Click(object sender, RoutedEventArgs e)
|
private void btnBack_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
|
|
||||||
NavigationService.Content = null;
|
NavigationService.Content = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void GoHome(object s, RoutedEventArgs e) => NavigationService.Content = null;
|
private void GoHome(object s, RoutedEventArgs e) => NavigationService.Content = null;
|
||||||
private void GoTest(object s, RoutedEventArgs e) => NavigationService.Content = new Views.PageTest();
|
private void GoTest(object s, RoutedEventArgs e) => NavigationService.Content = new Views.PageTest();
|
||||||
private void GoRecord(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordDate();
|
private void GoRecord(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordDate();
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ namespace 头罩视野.Views
|
|||||||
zdtsj1, zdtsj2, zdtsj3, zdtsj4, zdtsj5
|
zdtsj1, zdtsj2, zdtsj3, zdtsj4, zdtsj5
|
||||||
};
|
};
|
||||||
|
|
||||||
// 一次在UI线程批量更新,避免多次Invoke阻塞
|
// 一次在UI线程批量更新,避免多次Invoke阻塞0
|
||||||
await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||||
{
|
{
|
||||||
for (int i = 0; i < controls.Length; i++)
|
for (int i = 0; i < controls.Length; i++)
|
||||||
|
|||||||
Reference in New Issue
Block a user