如何在wpf c#类中获取我的xmal代码(如果附在下面)中的检查的cehckbox值

时间:2018-10-12 06:12:23

标签: wpf xaml

    <Grid Width="740">
        <!-- Left Side StackPanel to show Table and Search Delete Buttons-->
        <StackPanel Orientation="Vertical">
            <Grid Height="300" VerticalAlignment="Top" Margin="0 0 5 0">
                <DataGrid x:Name="dataGrid" Height="300" IsReadOnly="True" AutoGenerateColumns="False" Background="#FF2B2020" Foreground="White" VerticalAlignment="Top" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="auto">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="No." Binding="{Binding Path='ID'}" IsReadOnly="True" />
                        <DataGridTextColumn Header="Code" Binding="{Binding Path='SupplierCode'}" IsReadOnly="True" />
                        <DataGridTextColumn Header="Company Name" Binding="{Binding Path='CompanyName'}" IsReadOnly="True" />
                        <DataGridTextColumn Header="Contact Person" Binding="{Binding Path='ContactPerson'}" IsReadOnly="True" />
                        <DataGridTextColumn Header="Contact" Binding="{Binding Path='Contact'}" IsReadOnly="True" />
                        <DataGridTextColumn Header="Email" Binding="{Binding Path='Email'}" IsReadOnly="True" />

                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.Header>
                                <CheckBox x:Name="SelectAllCheckBox" ></CheckBox>
                            </DataGridTemplateColumn.Header>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate x:Name="dataTemplate">
                                    <CheckBox x:Name="CheckBox" Click="CheckBox_Click" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

                        <DataGridTemplateColumn Header="Update">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Name="EditButton" Content="Edit" Click="EditButton_Click" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>

我此功能正常工作... 我应该在 Click =“ CheckBox_Click” 函数中写什么 我想检查复选框天气与否 请帮忙!

我的 EditButton_Click 工作正常,我正在datagrid中获得编辑按钮...我想在每一行中获得与编辑按钮相同的复选框。并获得每个的价值

public void DataGridFunction()
        {
            string Query = "select * from Suppliers";
            SqlCommand command = new SqlCommand(Query, SqlCon);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable dt = new DataTable("Suppliers");
            adapter.Fill(dt);
            command.Dispose();
            adapter.Dispose();
            SqlCon.Close();
            dataGrid.ItemsSource = dt.DefaultView;
            adapter.Update(dt);
        }




private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataRowView dataRowView = (DataRowView)((Button)e.Source).DataContext;

                // setting values to fields
                HeaderLabel.Content = "Update Suppliers";
                Add.Content = "Update";
                IdLabel.Content = dataRowView[1].ToString();

                CNameTextBox.Text = dataRowView[2].ToString();
                CPersonTextBox.Text = dataRowView[3].ToString();
                RegTextBox.Text = dataRowView[4].ToString();
                ContactTextBox.Text = dataRowView[5].ToString();
                EmailTextBox.Text = dataRowView[6].ToString();
                AddressTextBox.Text = dataRowView[7].ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

1 个答案:

答案 0 :(得分:0)

您应该能够使用发件人获取值。

private void EditButton_Click(object sender, RoutedEventArgs e)
{
    var chkbox = sender as Checkbox;
    if (chkbox != null)
    {
        bool checked=chkbox.IsChecked.Value;
    }
}

此外,在复选框上使用Clicked事件时,我的行为也很奇怪。我几乎总是使用CheckedUnchecked事件。因此,如果您开始有奇怪的行为,建议您使用这两个事件,而不要使用Clicked