Как связать в wpf

Хорошо, у меня есть 2 сетки данных wpf, подобные этому

<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>

Что я хочу сделать, так это выбрать элемент в первой сетке данных, использовать этот элемент, чтобы найти данные, которые я хочу, во второй сетке данных. У меня есть метод, который может найти нужные данные, но я не знаю, как его связать, чтобы возвращаемое значение отображалось после выбора?

Метод

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

и свойства, к которым я привязываюсь в текущих сетках данных wpf

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");
    }
}

person user3240428    schedule 01.04.2014    source источник
comment
GetMetersOnCurrentCellID() это метод. В WPF вы можете связываться только со свойствами, а не с методами.   -  person Rohit Vats    schedule 01.04.2014


Ответы (1)


одним из способов было бы иметь словарь с правильным значением в одном словаре

 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}" />

или вы заполняете коллекцию своим методом и привязываете эту коллекцию к своей второй сетке

  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}" />
person blindmeis    schedule 01.04.2014