0

The problem is solved, Insted of using a dictionary I "converted" it to an OS (ObservableCollection) and bound it to a Datagrid inside the DatagridTemplateColumn

I am trying to add my ObservableCollaction which have a property Name and a dictionary with the properties: Name, AvgPrice and Shares.

How do I get the Name, AvgPrice and Shares inside the dictionary to a DataGrid?

The ObservableCollection:

private ObservableCollection<WatchlistModel> _watchlists;
public ObservableCollection<WatchlistModel> Watchlists
{
    get { return _watchlists; }
    set { _watchlists = value; OnPropertyChanged(); }
}

The WatchlistModel

private string _name;
public string Name
{
    get { return _name; }
    set { if(_name != value) { _name = value; OnPropertyChanged(); } }
}

private Dictionary<string, StockModel> _stocks;
public Dictionary<string, StockModel> Stocks
{
    get { return _stocks; }
    set { if (_stocks != value) { _stocks = value; OnPropertyChanged(); } }
}

The Datagrid

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Watchlists, UpdateSourceTrigger=PropertyChanged}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
        <DataGridTextColumn Header="Average Price" Binding="{Binding Path=AvgPrice}" />
        <DataGridTextColumn Header="Shares" Binding="{Binding Path=Shares}" />
    </DataGrid.Columns>
</DataGrid>

Json format

{
  "name": "My Watchlist",
  "stocks": {
    "Zaptec": {
      "avgPrice": "2,14",
      "name": "Zaptec",
      "shares": 121
    }
  }
}
Gnusson
  • 305
  • 1
  • 2
  • 9

1 Answers1

0

Use

    <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
<!-- Here you can add items control or whatever and bound its ItemSource to your 
Stocks -->
            </DataTemplate>
        </DataGridTemplateColumn>
    </DataGridTemplateColumn.CellTemplate>

But I don't recommend you to use Dictionary. Make ObservableCollection instead. It's pretty hard to Bind Dictionary<key, val> with observe on Items changed.

Melkor V
  • 66
  • 1
  • 7
  • Hello, the person who helped me with the dictionary told me that I have to use it... I made an edit and added the json format. It comes from firebase – Gnusson Apr 08 '21 at 13:40
  • U have a better way to deserialize it? – Gnusson Apr 08 '21 at 13:43
  • U can deserialize it in your StockModel, but create straighten class which contains that string (key) inside that class... – Melkor V Apr 08 '21 at 13:55
  • I do not think I understand what u mean unfortunately. You have an example? – Gnusson Apr 08 '21 at 13:57
  • Yeah it would be little messy, but it would be work without creating custom Dictionary which will Observe items change. But if you need exactly this Dictionary as reference - paste any Control with ItemSource property inside that comment section and use this solution https://stackoverflow.com/questions/10616554/observabledictionary-for-c-sharp – Melkor V Apr 08 '21 at 13:59
  • I mean just create new class with all properties from StockModel plus your key(string) from that Dictionary. Make ObservableCollection, add all StockModel objects into new Objects and insert them into ObservableCollection. – Melkor V Apr 08 '21 at 14:01
  • For example MyNewStockModel = new MyNewStockModel () { key = StockModel.key, avgPrice = StockModel[key].avgPrice, name = StockModel[key].Name, shares = StockModel[key].shares } – Melkor V Apr 08 '21 at 14:02
  • Then after your Stocks Dictionary created and filled with info - Fill your ObservableCollection with info from Stocks... – Melkor V Apr 08 '21 at 14:03
  • So you mean that I should create a ObservableCollection inside WatchlistModel? – Gnusson Apr 08 '21 at 14:03
  • @Gnusson exactly – Melkor V Apr 08 '21 at 14:05
  • Ok, so how do I fill the MyNewStockModel? – Gnusson Apr 08 '21 at 14:08
  • var observable = firebase.Child("users/" + _userService.GetUid()).AsObservable().Subscribe(d => – Gnusson Apr 08 '21 at 14:09
  • @Gnusson just get info from your Stocks Dictionary. Whenever you add new item to that dictionary - make sure to creane new MyNewStockModel with data from that camed to Stocks Dictionary - new object – Melkor V Apr 08 '21 at 14:11
  • Yes but I cannot Deserialize JsonConvert.DeserializeObject(json) Because this gives me an error – Gnusson Apr 08 '21 at 14:14
  • U have Discord or any other chat applications? Its hard to have a discussion here.. – Gnusson Apr 08 '21 at 14:15
  • @Gnusson add [JsonIgnore] to ObservableCollection. When Deserialization ends - fill ObservableCollection with data – Melkor V Apr 08 '21 at 14:16
  • @Gnusson Timur#0378 Discord – Melkor V Apr 08 '21 at 14:17