WPF实现一个表格数据从cs获取动态渲染

  1. xaml代码
xml 复制代码
<Window x:Class="YourNamespace.MainWindow"  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        Title="MainWindow" Height="350" Width="525">  
    <Grid>  
        <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Products}">  
           <DataGrid.Resources>
	         <!--设置表头样式-->
             <Style TargetType="{x:Type DataGridColumnHeader}">
                 <Setter Property="HorizontalContentAlignment" Value="Center"/>
                 <Setter Property="VerticalAlignment" Value="Center"/>
                 <Setter Property="FontSize" Value="16"/>
                 <Setter Property="Padding" Value="0 10"/>
             </Style>
             <!--设置行样式-->
             <Style x:Key="CenteredTextBlockStyle" TargetType="{x:Type TextBlock}">
                  <Setter Property="VerticalAlignment" Value="Center"/>
                  <Setter Property="HorizontalAlignment" Value="Center"/>
                  <Setter Property="FontSize" Value="16"/>
              </Style>
            </DataGrid.Resources>
	    	<DataGrid.Columns>  
		        <DataGridTextColumn Header="Id" Binding="{Binding Id}" />  
		        <!--设置超出自动换行并显示滚动条-->
		        <DataGridTemplateColumn Header="Name" Width="2*">
         <DataGridTemplateColumn.CellTemplate >
            <DataTemplate>
            <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource CenteredTextBlockStyle}"/>
            </ScrollViewer>
            </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>
		  <DataGridTextColumn Header="Price" Binding="{Binding Price}" />  
		<DataGridTemplateColumn Header="操作" Width="100">
		  <!--设置表格内的一个按钮-->
        <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Button Content="编辑" Cursor="Hand" Click="editButton_Dianji" >
                    <Button.Resources>
                        <Style TargetType="{x:Type Border}">
                            <Setter Property="CornerRadius" Value="5"/>
                        </Style>
                    </Button.Resources>
                </Button>
            </StackPanel>
         </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
	    	</DataGrid.Columns>  
		</DataGrid>
    </Grid>  
</Window>

2.cs代码

csharp 复制代码
public ObservableCollection<Product> Products { get; set; }  
  
public MainWindow()  
{  
    InitializeComponent();  
    this.DataContext = this; // 设置数据上下文为当前窗口实例 
    // 初始化产品集合  
    Products = new ObservableCollection<Product>  
    {  
        new Product { Id = 1, Name = "Product 1", Price = 10.99M },  
        new Product { Id = 2, Name = "Product 2", Price = 20.99M }  
        // 添加更多产品...  
    };  
}
相关推荐
GuWenyue6 分钟前
不用第三方SDK!Vue3原生Fetch实现DeepSeek流式输出,90%前端都会踩的分片解析坑一次性解决
前端·人工智能·llm
独隅8 分钟前
前端离线暂停更新策略:Service Worker 与 PWA 实战指南
前端
不如摸鱼去35 分钟前
Wot UI 2.3.0 发布:二维码组件来了,Open Wot 与 wot-starter 同步更新
前端·ui·微信小程序·前端框架·uni-app
Cloud_bread1 小时前
从ReAct到自主闭环:Agentic Coding核心执行引擎的技术演进
前端·react.js·前端框架
cidy_981 小时前
OptMem 使用教程
前端
杉氧1 小时前
用 Compose 挑战交互与动效天花板:ComposeCraftLab 开源实验室全解析
android·前端·kotlin
Canace1 小时前
AI 生成到 90% 突然断了:你的解决方案是?
前端·人工智能
TinssonTai1 小时前
Vite 8 版 Chrome 插件全家桶,popup/options/sidepanel 一次集齐
前端·vue.js
玉鸯1 小时前
让 Agent 面向用户:AG-UI 协议构建 Agent 前端
前端·python·agent
程序员黑豆2 小时前
鸿蒙应用开发 @Extend 装饰器使用教程
前端·harmonyos