C# 如何在wpf中绑定

C# 如何在wpf中绑定,c#,wpf,datagrid,C#,Wpf,Datagrid,好的,我有两个像这样的wpf数据网格 <GroupBox Header="Generel" Grid.Row="0"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="53*"/> <ColumnDefinition Width="86*"/> </Grid.ColumnDefi

好的,我有两个像这样的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();
}
public List GetMetersOnCurrentCellID()
{
返回仪表。其中(x=>x.Gsmdata.Last()
.CellID==当前CellID.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");
    }
}
公钥值对CurrentCellID
{
获取{return currentCellID;}
设置
{
currentCellID=值;
OnPropertyChanged(“CurrentCellID”);
}
}
公共字典CellIDMeterCount
{
获取{return cellidMeterCount;}
设置
{
cellidMeterCount=数值;
OnPropertyChanged(“CellIDMeterCount”);
}
}

一种方法是在一个字典中有一个具有正确值的字典

 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}" />
公共字典CellIDMeterList{get;set;}
或者使用方法填充集合,并将这些集合绑定到第二个网格

  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}" />
public OberservableCollection MyMeter{get;set;}
public void getMeterOnCurrentCellId()
{
var l=meters.Where(x=>x.Gsmdata.Last()
.CellID==当前CellID.Key)
.ToList();
MyMeter.Clear();
foreach(l中的var项目)
MyMeter.添加(项目);
}

GetMetersOnCurrentCellID()
是方法。在WPF中,只能使用属性绑定,不能使用方法绑定。