首页 > 其他分享 >wpf Datagrid绑定 Dictionary

wpf Datagrid绑定 Dictionary

时间:2022-12-01 10:44:07浏览次数:56  
标签:Liststring Dictionary column Binding Dic Datagrid Add new wpf

原文网址: https://www.cnblogs.com/zany-hui/articles/7623874.html

数据源格式:Dictionary<int, List<string>>

1.通过xaml设置

<DataGrid x:Name="dg" ItemsSource="{Binding Dic}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="id" Binding="{Binding Key}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Value[0]}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Value[1]}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Value[2]}"/>
</DataGrid.Columns>
</DataGrid>

复制代码
1  <DataGrid x:Name="dg" ItemsSource="{Binding Dic}" AutoGenerateColumns="False">
2             <DataGrid.Columns>
3                 <DataGridTextColumn Header="id" Binding="{Binding Key}"/>
4                 <DataGridTextColumn Header="Name" Binding="{Binding Value[0]}"/>
5                 <DataGridTextColumn Header="Name" Binding="{Binding Value[1]}"/>
6                 <DataGridTextColumn Header="Name" Binding="{Binding Value[2]}"/>
7             </DataGrid.Columns>
8         </DataGrid>
复制代码

 2.通过代码设置

DataGridTextColumn column = new DataGridTextColumn();
column.Header = "name4";
column.Binding = new Binding("Value[3]");
dg.Columns.Add(column);

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <DataGrid x:Name="dg" ItemsSource="{Binding Dic}" AutoGenerateColumns="False">     <DataGrid.Columns>       <DataGridTextColumn Header="id" Binding="{Binding Key}"/>     </DataGrid.Columns> </DataGrid>      private Dictionary<int, List<string>> dic;    public Dictionary<int, List<string>> Dic  {      get return dic; }      set { dic = value; }  }    public MainWindow()  {      InitializeComponent();      this.DataContext = this;      Dic = new Dictionary<int, List<string>>();      Dic.Add(1, new List<string> { "a""b""c""5" });      Dic.Add(2, new List<string> { "d" });      Dic.Add(3, new List<string> { "e""f" });        int count = 0;      foreach (List<string> lst in Dic.Values)      {          if (lst.Count > count)          {                for (int i = count; i < lst.Count; i++)              {                  DataGridTextColumn column = new DataGridTextColumn();                  column.Header = "name" + i;                  column.Binding = new Binding(string.Format("Value[{0}]", i));                  dg.Columns.Add(column);              }              count = lst.Count;          }      }  }

  

标签:Liststring,Dictionary,column,Binding,Dic,Datagrid,Add,new,wpf
From: https://www.cnblogs.com/bruce1992/p/16940686.html

相关文章

  • WPF中单位
    WPF中单位WPF程序中的单位是与设备无关的单位,每个单位是1/96英寸,如果电脑的DPI(DotsPerInch,每英寸点数)设置为96(每个英寸96个像素),那么此时每个WPF单位对应一个像素,不过如果......
  • WPF 四种尺寸单位
    像素 px默认单位可以省略厘米cm英寸in点pt1in=96px1cm=96/2.42px1pt=96/72px   postedon 2018-08-3011:56 NET未来之路 阅读( ...)评论(......
  • WPF在国内还有希望和前途吗?
    WPF在国内还有希望和前途吗? 为什么大公司不会使用它?QQ出了个概念版还很少更新,wpf是不是没前途了? 作者:匿名用户链接:https://www.zhihu.com/question/26862663/an......
  • .net core/5/6/7中WPF如何优雅的开始开发
    .netcore/5/6/7中WPF如何优雅的开始开发 WPF是微软的.net平台中的一个桌面客户端应用程序框架,经常用于企业开发windows桌面客户端,广泛应用于中小企业快速开发一款......
  • C#数据结构--Dictionary、HashTable、List、HashSet区别
    在.Net  模仿java的过程中,抛弃了HashMap,所以我们今天分析下Dictionary、HashTable、HashSet区别。处理碰撞,即碰撞到同一个Bucket槽上:Hashtable和Dictionary从数据结构上......
  • C#数据结构-Dictionary实现
    在看过一篇大神写的​​《带你看懂Dictionary的内部实现》​​,对Dictionary的内部实现有了一个清晰的了解,但纸上得来终觉浅,作为程序员,还是自己调试一下代码,记忆更深刻,为此专......
  • WPF_MVVM框架(5)
    1.NuGet引用MVVM框架包引入该框架包之后,默认会在目录下创建ViewModel层的示例代码2.第二步,通过在MainViewModel中创建一些业务代码,将其与MainWindow.xaml通过......
  • WPF控件模板(6)
    什么是ControlTemplate?ControlTemplate(控件模板)不仅是用于来定义控件的外观、样式,还可通过控件模板的触发器(ControlTemplate.Triggers)修改控件的行为、响应动画等......
  • c# datagridview使用DGData_CellValueNeeded 填入数据,给某个单元格附背景颜色
    privatevoidDGData_CellValueNeeded(objectsender,DataGridViewCellValueEventArgse){    DataRowdr=DTData.Rows[e.RowIndex];    for......
  • 界面控件DevExpress WPF的主题设计器,可轻松完成应用主题研发
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专......