5/22/2010

DataGridとの憂鬱

どうもJudaです。
MainPage.xaml.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using System.Reflection;  
  13.   
  14. namespace AgScheduler  
  15. {  
  16.  public partial class MainPage : UserControl  
  17.  {  
  18.   public MainPage()  
  19.   {  
  20.    InitializeComponent();  
  21.    this.Loaded += new RoutedEventHandler(MainPage_Loaded);  
  22.    var list = DataEntity.Users;  
  23.    PropertyInfo[] infos = typeof(UserInfo).GetProperties();  
  24.    //var q = (from rec in list  
  25.    //         select new { Name = rec.名前, Norma = rec.年計, Result = rec.実績 })  
  26.    //         .ToArray();  
  27.    this.DataContext = infos.AsQueryable();  
  28.   
  29.   }  
  30.   
  31.   void MainPage_Loaded(object sender, RoutedEventArgs e)  
  32.   {  
  33.    PropertyInfo[] infos = typeof(UserInfo).GetProperties();  
  34.    this.dataGrid1.RowHeight = 30;  
  35.    this.Foreground = new SolidColorBrush(Colors.Red);  
  36.   }  
  37.   
  38.   
  39.   private void Button_Click(object sender, RoutedEventArgs e)  
  40.   {  
  41.   
  42.   }  
  43.   
  44.   private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  45.   {  
  46.   
  47.   }  
  48.  }  
  49.  public class hoge  
  50.  {  
  51.   public string Name { getset; }  
  52.   public double Norma { getset; }  
  53.   public double Result { getset; }  
  54.  }  
  55. }  

MainPage.xaml
  1. <UserControl  
  2.     x:Class="AgScheduler.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  6.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  7.     xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"  
  8.     xmlns:ctrl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
  9.     mc:Ignorable="d"  
  10.     d:DesignHeight="600" d:DesignWidth="800">  
  11.     <Grid x:Name="LayoutRoot">  
  12.         <Grid.ColumnDefinitions>  
  13.             <ColumnDefinition Width="180" />  
  14.             <ColumnDefinition Width="Auto" />  
  15.             <ColumnDefinition Width="620*" />  
  16.         </Grid.ColumnDefinitions>  
  17.         <ctrl:DataGrid Name="dataGrid1"  
  18.                        ItemsSource="{Binding}" Grid.Column="2" Margin="0" AutoGenerateColumns="True"  
  19.                        SelectionChanged="dataGrid1_SelectionChanged" />  
  20.         <Button Content="Help" Click="Button_Click"/>  
  21.     </Grid>  
  22. </UserControl>  

DataEntity.cs
  1. using System;  
  2. using System.Net;  
  3. using System.Windows;  
  4. using System.Windows.Controls;  
  5. using System.Windows.Documents;  
  6. using System.Windows.Ink;  
  7. using System.Windows.Input;  
  8. using System.Windows.Media;  
  9. using System.Windows.Media.Animation;  
  10. using System.Windows.Shapes;  
  11. using System.Collections.Generic;  
  12. using System.Linq;  
  13.   
  14. namespace AgScheduler  
  15. {  
  16.  public class DataEntity  
  17.  {  
  18.   public static UserInfo CreateUserInfo()  
  19.   {  
  20.    return new UserInfo()  
  21.    {  
  22.     ID = 0,  
  23.     実績 = 80,  
  24.     年計 = 1200,  
  25.     名前 = "テスト太郎"  
  26.    };  
  27.   }  
  28.   public static IQueryable<userinfo> Users {  
  29.    get  
  30.    {  
  31.     var list = new List<userinfo>();  
  32.     list.AddRange(new UserInfo[]{  
  33.      CreateUserInfo(),  
  34.      CreateUserInfo(),  
  35.      CreateUserInfo(),  
  36.      CreateUserInfo(),  
  37.      CreateUserInfo(),  
  38.      CreateUserInfo(),  
  39.      CreateUserInfo(),  
  40.      CreateUserInfo()  
  41.     });  
  42.     return list.AsQueryable();  
  43.    }  
  44.    set { }  
  45.   }  
  46.   public static IQueryable<projectinfo> Projects { getset; }  
  47.  }  
  48.  public class UserInfo  
  49.  {  
  50.   public int ID { getset; }  
  51.   public string 名前 { getset; }  
  52.   public double 年計 { getset; }  
  53.   public double 実績 { getset; }  
  54.  }  
  55.  public class ProjectInfo  
  56.  {  
  57.   public int ID { getset; }  
  58.   public string ProjectName { getset; }  
  59.   public double ProjectAmount { getset; }  
  60.  }  
  61. }  
  62. </projectinfo></userinfo></userinfo>  


明示的なデータ構造がわかるものじゃないと中身を表示してくれないし、そもそもPropertyInfoは中身を表示してくれない。意味がわからない。無名構造体でも表示はしてくれない。どういうことだ。適応のさせかたのどこかで間違えたことは確かなのだが、どこかわからない。無名構造体が使えないのでは、DataGridに動的にデータを適応しようと思うと困るのだが。

0 件のコメント:

コメントを投稿