从Datagrid获取已检查的行

时间:2012-04-10 13:47:07

标签: datagrid

我有一个带有DataGridCheckBoxColumn的绑定数据网格,我想获取选中复选框的行。

我的代码就像这样

<sdk:DataGrid x:Name="McDataGrid" Width="580" Height="250"
                   Margin="10,10,0,0" 
                   ColumnWidth="100" RowHeight="30"
                   GridLinesVisibility="Vertical"                        
                   SelectionMode="Single"
                   BorderThickness="5"
                   AutoGenerateColumns="False"
                   AreRowDetailsFrozen="True"
                   HorizontalGridLinesBrush="Black"
                   VerticalGridLinesBrush="Black"
                   RowStyle="{StaticResource MyCustomRow}"
                   RowBackground="LightGray" AlternatingRowBackground="LightGray"
                >
                <!--Background="LightGray" RowBackground="LightYellow"
                AlternatingRowBackground="LightBlue"
                 BorderBrush="Gray" -->

                <!-- Add DataGrid Columns -->
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn Header="SL" Width="40" Binding="{Binding ID}" FontSize="12" />

                    <!--<sdk:DataGridTextColumn Header="Road Name" Width="100" Binding="{Binding RoadName}" FontSize="12" />-->

                    <sdk:DataGridTemplateColumn Header="Road Name" Width="300">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextWrapping="Wrap" Text="{Binding RoadName}" MaxHeight="200" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>

                    <sdk:DataGridTextColumn Header="Start KM" Width="70" Binding="{Binding StartKM}" FontSize="12" />

                    <sdk:DataGridTextColumn Header="End KM" Width="70" Binding="{Binding EndKM}" FontSize="12" />

                    <sdk:DataGridCheckBoxColumn Header="Select"  Binding="{Binding Available}" />

                </sdk:DataGrid.Columns>

            </sdk:DataGrid>


        public class IntersectionRoads
    {
        public int ID { get; set; }
        public string RoadName { get; set; }
        public string StartKM { get; set; }
        public string EndKM { get; set; }
        public bool Available { get; set; }
    }

McDataGrid.ItemsSource = intersectingRoads;

我想获取选中复选框的所有行值 如果我在这里做错了也请指导

期待回复 提前致谢 :)

1 个答案:

答案 0 :(得分:0)

我明白了,这就是诀窍:

string strRoadIDs = string.Empty;
foreach (IntersectionRoads selectedroads in McDataGrid.ItemsSource)
{
    if (selectedroads.Available.Equals(true))
    {
        strRoadIDs = strRoadIDs + selectedroads.RoadKeyID + ",";
    }
}