从DataGrid Selection_Change事件获取多个属性

时间:2012-04-23 20:06:54

标签: c# wpf xaml

这是我的问题

我有一个DataGrid,这是他的来源

var Query = from a in m.Table1
                       join p in m.Table2
                       on a.Value1 equals p.Value1
                       join c in m.Table3 on a.Value2 equals  c.Value2 
           select new {
                        value1 = a.value1
                        value2 = p.value2
                        value3 = c.value3
                        value4 = a.value4

                      };
    <DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="84,75,0,0"
 Name="Grid_Exmple" Width="478" Height="219" VerticalAlignment="Top" SelectionChanged="Grid_Exmple_SelectionChanged">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding value1 }" Header="value1 " IsReadOnly="True" x:Name="dgrvalue1 "  />
                        <DataGridTextColumn Binding="{Binding value2 }" Header="value2 " IsReadOnly="True"  x:Name="dgrvalue2 " />
                        <DataGridTextColumn Binding="{Binding value3 }" Header="value3 " IsReadOnly="True"  x:Name="dgrvalue3" />
                        <DataGridTextColumn Binding="{Binding value4}" Header="value4" IsReadOnly="True"  x:Name="dgrvalue4" />
                    </DataGrid.Columns>

当我们按下DataGrid选择事件时

  private void Grid_Exmple_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    Here I try To Get The properties 

我可以看到e.AddedItems中的属性,但我无法投射它

    ((<>f__AnonymousType2<int,int?,decimal?,decimal?,string,string,string,int,string>)(((object[])(e.AddedItems))[0])).Value1

  }

1 个答案:

答案 0 :(得分:2)

至少有两种可能性:

  1. 创建一个命名类型,用它填充网格,然后再回到此类型。
  2. 转到dynamic并做任何你想做的事。