WPF中判断DataGrid增加复选框和头部全选,以及判断哪一行的checkbox被选中

直接代码,不废话

<my:DataGrid AutoGenerateColumns="False" IsReadOnly="True" Margin="271,35,12,40" Name="dataGrid1" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" Grid.Row="1">
            <my:DataGrid.Columns>
                <my:DataGridTemplateColumn Header="全选/全不选" Width="110">
                    <my:DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="my:DataGridColumnHeader">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <GradientStop Color="White" Offset="0" />
                                        <GradientStop Color="LightPink" Offset="0.5" />
                                        <GradientStop Color="White" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="14" />
                        </Style>
                    </my:DataGridTemplateColumn.HeaderStyle>
                    <my:DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <CheckBox Checked="ckbSelectedAll_Checked" Margin="10,0" Name="ckbSelectedAll" Unchecked="ckbSelectedAll_Unchecked">全选/全不选</CheckBox>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.HeaderTemplate>
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox HorizontalAlignment="Center" IsChecked="{Binding IsChecked, Mode=TwoWay}" VerticalAlignment="Center" />
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
                <my:DataGridTextColumn Binding="{Binding xx}" Header="xx">
                    <my:DataGridTextColumn.HeaderStyle>
                        <Style TargetType="my:DataGridColumnHeader">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <GradientStop Color="White" Offset="0" />
                                        <GradientStop Color="LightGreen" Offset="0.5" />
                                        <GradientStop Color="White" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="14" />
                        </Style>
                    </my:DataGridTextColumn.HeaderStyle>
                </my:DataGridTextColumn>
                <my:DataGridTextColumn Binding="{Binding xx}" Header="xx">
                    <my:DataGridTextColumn.HeaderStyle>
                        <Style TargetType="my:DataGridColumnHeader">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <GradientStop Color="White" Offset="0" />
                                        <GradientStop Color="Yellow" Offset="0.5" />
                                        <GradientStop Color="White" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="14" />
                        </Style>
                    </my:DataGridTextColumn.HeaderStyle>
                </my:DataGridTextColumn>
            </my:DataGrid.Columns>
        </my:DataGrid>

 

 

        private void ckbSelectedAll_Checked(object sender, RoutedEventArgs e)
        {
            foreach (DataRow row in dt.Rows)
            {
                row["IsChecked"] = true;
            }
        }

        private void ckbSelectedAll_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (DataRow row in dt.Rows)
            {
                row["IsChecked"] = false;
            }
        }

 

当然,华丽的效果,还是要配合美工实现,可是我目前只有一个人孤军奋战,而且以前没接触过c#,只花了三四天时间看了下c#和wpf,写的很简陋,不要见笑。我还在继续学习中。(之所以只花了三四天时间看了下,是因为我更喜欢实际动手做一下。我知道,我从来不擅长于考试、理论之类的东西,但我也十分的清楚,我能理解并将其付之于实践,这就是我的强项。)

 


出处:https://blog.csdn.net/Alsmile/article/details/6593440

=========================================================================

WPF中判断dataGrid的哪一行的checkbox被选中

先看看界面:

 

代码:

<DataGridCheckBoxColumn Header="操作" Width="10*" Binding="{Binding IsChecked}"/>
<DataGridTextColumn Header="id" Width="10*" Binding="{Binding Path=ui_id, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden"/>
     <DataGridTextColumn Header="工号" Width="12*" Binding="{Binding Path=ui_number, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
     <DataGridTextColumn Header="姓名" Width="10*" Binding="{Binding Path=ui_name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
......

 

 点击最上面的【删除】后,如何判断是哪一项呗选中?求指教!!!
后台代码:
            int count = dataGrid1.Items.Count;
            DataRowView[] drv = new DataRowView[count];
            DataTable dt = (dataGrid1.ItemsSource as DataView).Table;
            
            for (int i = 0; i < count; i++)
            {
                 drv[i] = dataGrid1.Items[i] as DataRowView;
            }

 

你datagrid的数据是绑定的模型的话 直接模型设置个属性,然后datatemplate里的checkbox绑定这个属性,双向的,然后点删除的时候从你的数据集合里判断哪个模型的这个属性被设置为true就可以了

 

 

出处:https://bbs.csdn.net/topics/391875244

========================================================================

 

posted on 2020-03-25 19:37  jack_Meng  阅读(...)  评论(...编辑  收藏

导航