user3240428
user3240428

Reputation: 121

How to bind in wpf

Ok I have 2 wpf datagrids like this

<GroupBox Header="Generel" Grid.Row="0">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="53*"/>
            <ColumnDefinition Width="86*"/>
        </Grid.ColumnDefinitions>
        <DataGrid Name="dgCellIDMeterCount" 
                  ItemsSource="{Binding Path=CellIDMeterCount}" 
                  Margin="0,0,0,0" 
                  AutoGenerateColumns="False" 
                  HorizontalAlignment="Left" 
                  SelectionMode="Single" 
                  SelectedItem="{Binding Mode=TwoWay, Path=CurrentCellID}" 
                  Grid.Row="0" 
                  Grid.ColumnSpan="1" 
                  CellStyle="{StaticResource DataGridCellStyle}" 
                  IsReadOnly="True" Width="100">
            <DataGrid.Columns>
                <DataGridTextColumn Header="CellID" Binding="{Binding Key}"/>
                <DataGridTextColumn Header="Meters" Binding="{Binding Value}"/>
            </DataGrid.Columns>
        </DataGrid>
        <DataGrid Name="dgMetersOnCellID" 
                  ItemsSource="{Binding Path=CurrentCellID.GetMetersOnCurrentCellID}"           
                  Margin="10,0,0,0" 
                  AutoGenerateColumns="False" 
                  HorizontalAlignment="Left" 
                  SelectionMode="Single" 
                  CellStyle="{StaticResource DataGridCellStyle}" 
                  IsReadOnly="True" 
                  Width="100" 
                  Grid.Column="1"/>
    </Grid>
</GroupBox>

What I want to do is Selecting an item in the first datagrid, use that item to find the data I want in the second datagrid. I have a method that can find the right data but i dont know how to bind it so the return value get showed after the selection has happend?

The method

public List<Meter> GetMetersOnCurrentCellID()
{
    return meters.Where(x => x.Gsmdata.Last()
                 .CellID == CurrentCellID.Key)
                 .ToList();
}

and the properties I bind to in the current wpf datagrids

public KeyValuePair<int, int> CurrentCellID
{
    get { return currentCellID; }
    set
    {
        currentCellID = value;
        OnPropertyChanged("CurrentCellID");
    }
}

public Dictionary<int, int> CellIDMeterCount
{
    get { return cellidMeterCount; }
    set
    {
        cellidMeterCount = value;
        OnPropertyChanged("CellIDMeterCount");
    }
}

Upvotes: 0

Views: 84

Answers (1)

blindmeis
blindmeis

Reputation: 22435

one way would be to have a Dictionary with the right value in one dictionary

 public Dictionary<int, List<Meter>> CellIDMeterList {get;set;}

 <DataGrid Name="1stGrid" ItemsSource="{Binding Path=CellIDMeterList }" />

  <DataGrid Name="2ndGrid" ItemsSource="{Binding ElementName=1stGrid, Path=SelectedItem.Value}" />

or you fill a collection with your method and bind these Collection to your 2nd grid

  public OberservableCollection<Meter> MyMeter {get;set;}

  public void GetMetersOnCurrentCellID()
  {
      var l = meters.Where(x => x.Gsmdata.Last()
             .CellID == CurrentCellID.Key)
             .ToList();

      MyMeter.Clear();
      foreach(var item in l)
         MyMeter.Add(item);
  }

  <DataGrid Name="2ndGrid" ItemsSource="{Binding Path=MyMeter}" />

Upvotes: 1

Related Questions