【问题标题】:c# Show dictionary with specific value in datagridc#在datagrid中显示具有特定值的字典
【发布时间】:2016-10-01 18:46:23
【问题描述】:

我有一个特定的 json 文件:

    {
    "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"},
{

我的 DataGrid 带有绑定:

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

使用 ViewModel:

    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。我希望它只显示来自Time dictionary 的 1 个特定值。就像我从DatePicker 或其他东西中选择日期一样,DataGrid 仅显示specific key 的值。我一直在尝试使用foreach loop 进行操作,找到特定的密钥并删除其他密钥,但这不起作用/我做错了什么。

【问题讨论】:

    标签: c# wpf dictionary datagrid


    【解决方案1】:

    看起来你有一个主从关系。 看看Binding (MSDN)的文档

    尝试为您选择的 ProcessInfo (Master) 添加附加属性

    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>
    

    【讨论】:

      猜你喜欢
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 2012-11-29
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      相关资源
      最近更新 更多