c # Показать словарь с определенным значением в datagrid

#c# #wpf #dictionary #datagrid

#c# #wpf #словарь #datagrid

Вопрос:

У меня есть конкретный файл 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 . И я хочу, чтобы он показывал только 1 конкретное значение из Time dictionary . Например, я выбираю дату из DatePicker или какую-то другую вещь и DataGrid показываю только значение с этим specific key . Я пытался сделать это с foreach loop помощью, найти конкретный ключ и удалить другие, но это не сработало / я сделал что-то не так.

Ответ №1:

похоже, у вас есть отношение master-detail. посмотрите документацию по привязке (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>