c#在datagrid中显示具有特定值的字典

时间:2016-10-01 18:46:24

标签: c# wpf dictionary datagrid

我有一个特定的json文件:

  server {

  listen 0.0.0.0:80;
  listen 0.0.0.0:443 ssl;
  root /usr/share/nginx/html;
  index index.html index.htm;

  ssl on;
  sslcertificate /etc/ssl/certs/ssl-bundle.crt;
  sslcertificatekey /etc/ssl/private/budokai-onlinecom.key;
  sslciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-        SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kED$
  ssldhparam /etc/ssl/private/dhparmas.pem;
  sslpreferserverciphers on;
  sslprotocols TLSv1 TLSv1.1 TLSv1.2;

  if ($sslprotocol = "") {
  rewrite ^ https://$host$requesturi? permanent;

  }
 largeclientheader_buffers 8 32k;
 location / {

 proxyhttpversion 1.1;

 proxysetheader Accept-Encoding "";
 proxysetheader X-Real-IP $remoteaddr;
 proxysetheader Host $host;
 proxysetheader X-Forwarded-For $proxyaddxforwardedfor;
 proxysetheader XFORWARDEDPROTO https;
 proxysetheader X-NginX-Proxy true;
 proxybuffers 8 32k;
 proxybuffersize 64k;
 proxysetheader Upgrade $httpupgrade;
 proxysetheader Connection "Upgrade";
 proxyreadtimeout 86400;
 proxypass http://budokai-online.com:8080 ;

}

我的DataGrid带有绑定:

    {
    "Time":{
        "2016-10-01":"00:00:10",
        "2016-10-02":"00:00:20",
        "2016-10-03":"00:00:30",
    },
    "Id":2,
    "Group":"Not found",
    "Name":"XXX"},
{

使用ViewModel:

<DataGrid ItemsSource="{Binding Path=ProcessListTable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="True"/>

private List<ProcessInfo> listTable; public List<ProcessInfo> ProcessListTable { get { listTable = JsonConvert.DeserializeObject<List<ProcessInfo>>(File.ReadAllText(pathToFile)); return listTable; } set { listTable = value; OnPropertyChanged(nameof(ProcessListTable)); } } 正在显示DataGrid。我希望它只显示Id, Group and Name中的1个特定值。就像我从Time dictionary或其他事情中选择日期一样,DatePicker仅显示DataGrid的值。我一直试图用specific key来做,找到特定的密钥并删除其他密钥,但那不起作用/我做错了什么。

1 个答案:

答案 0 :(得分:0)

看起来你有一个主 - 细节关系。 查看Binding (MSDN)

的文档

尝试为您选择的ProcessInfo(主)添加其他属性

public List<ProcessInfo> ProcessListTable
{
    get { return listTable?? (listTable = JsonConvert.DeserializeObject<List<ProcessInfo>>(File.ReadAllText(pathToFile));); }
}

public ProcessInfo SelectedProcessItem
{
    get { return _selectedProcessItem; }
    set
    {
        if (Equals(value, _selectedProcessItem)) return;
        _selectedProcessItem = value;
        OnPropertyChanged();
    }
}

并更改您的XMAL 带有额外的细节网格

<Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <!-- Master Grid -->
                <DataGrid Grid.Row="0" x:Name="MasterGrid"
                          ItemsSource="{Binding Path=ProcessListTable, Mode=OneWay}" 
                          SelectedItem="{Binding Path=SelectedProcessItem, Mode=TwoWay}"
                          IsSynchronizedWithCurrentItem="True"
                          AutoGenerateColumns="True"/>
                <!-- Detail Grid -->
                <DataGrid Grid.Row="1" ItemsSource="{Binding Path=SelectedProcessItem.Time, Mode=OneWay}" 
                          IsSynchronizedWithCurrentItem="True"
                          AutoGenerateColumns="True"/>
</Grid>