2014-04-01 40 views
0

好吧,我有2個WPF這樣如何在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> 

數據網格我想要做的就是選擇在第一個DataGrid的項目,使用該項找我想在第二個DataGrid中的數據。我有一個方法可以找到正確的數據,但我不知道如何綁定它,以便在選擇發生後顯示返回值?

方法

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

'GetMetersOnCurrentCellID()'是方法。在WPF中,你只能綁定屬性而不是方法。 –

回答

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

或者你用你的方法填充一個集合並將這些Collection綁定到你的第二個網格

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