关于datagrid中CheckBox的问题

发布于 2024-11-27 03:03:37 字数 2001 浏览 1 评论 0原文

重复每个 MessageBox.Show(this.myProduct.Radif.ToString());!!!!

发生什么事了?

xaml代码:

 <dg:DataGrid.Columns>
            <dg:DataGridTextColumn Binding="{Binding IdBook}" Header="IdBook" ></dg:DataGridTextColumn>
            <dg:DataGridTextColumn Binding="{Binding NameBook}" Header="NameBook"></dg:DataGridTextColumn>
            <dg:DataGridTextColumn Binding="{Binding Author}" Header="Author"></dg:DataGridTextColumn>
            <dg:DataGridTextColumn Binding="{Binding Price}" Header="Price"></dg:DataGridTextColumn>
            <dg:DataGridTextColumn Binding="{Binding DateRegister}" Header="DateRegister"></dg:DataGridTextColumn>
            <dg:DataGridTextColumn Binding="{Binding Description}" Header=Description" Width="*"></dg:DataGridTextColumn>
            <dg:DataGridTemplateColumn>
                <dg:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Selecting, UpdateSourceTrigger=PropertyChanged}" Checked="Checked" Unchecked="UnChecked" />
                    </DataTemplate>
                </dg:DataGridTemplateColumn.CellTemplate>
            </dg:DataGridTemplateColumn>

我的代码:

    private List<Book> MyProd = new List<Book>();
    private Book myProduct = null;

    private void Checked(object sender, RoutedEventArgs e)
    {
        this.myProduct = new Book();
        this.myProduct = (Book)dataGrid.CurrentItem;
        MessageBox.Show(this.myProduct.No.ToString());
    }

class Book
{
    public int No{ get; set; }
    public string NameBook { get; set; }
    public string Author { get; set; }
    public string Description { get; set; }
    public string DateRegister { get; set; }
    public Int64 Price { get; set; }
    public bool Selecting { get; set; }
}

Repeats every MessageBox.Show(this.myProduct.Radif.ToString());!!!!

What's happening?

xaml code :

 <dg:DataGrid.Columns>
            <dg:DataGridTextColumn Binding="{Binding IdBook}" Header="IdBook" ></dg:DataGridTextColumn>
            <dg:DataGridTextColumn Binding="{Binding NameBook}" Header="NameBook"></dg:DataGridTextColumn>
            <dg:DataGridTextColumn Binding="{Binding Author}" Header="Author"></dg:DataGridTextColumn>
            <dg:DataGridTextColumn Binding="{Binding Price}" Header="Price"></dg:DataGridTextColumn>
            <dg:DataGridTextColumn Binding="{Binding DateRegister}" Header="DateRegister"></dg:DataGridTextColumn>
            <dg:DataGridTextColumn Binding="{Binding Description}" Header=Description" Width="*"></dg:DataGridTextColumn>
            <dg:DataGridTemplateColumn>
                <dg:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Selecting, UpdateSourceTrigger=PropertyChanged}" Checked="Checked" Unchecked="UnChecked" />
                    </DataTemplate>
                </dg:DataGridTemplateColumn.CellTemplate>
            </dg:DataGridTemplateColumn>

my code :

    private List<Book> MyProd = new List<Book>();
    private Book myProduct = null;

    private void Checked(object sender, RoutedEventArgs e)
    {
        this.myProduct = new Book();
        this.myProduct = (Book)dataGrid.CurrentItem;
        MessageBox.Show(this.myProduct.No.ToString());
    }

class Book
{
    public int No{ get; set; }
    public string NameBook { get; set; }
    public string Author { get; set; }
    public string Description { get; set; }
    public string DateRegister { get; set; }
    public Int64 Price { get; set; }
    public bool Selecting { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘人皮目录ツ 2024-12-04 03:03:37

Checkbox 的 Checked / Unchecked 事件实际上是原始的 ToggleButton.Checked / Unchecked 类型,开发人员社区不鼓励使用它们。

相反,您可以处理 CheckBox.Click 事件并在该处理程序内部...。

 if (((CheckBox)sender).IsChecked.GetValueOrDefault(false))
 {
       //// Your Checked Code Here ...
 }
 else
 {
      //// Your unchecked code here ...
 }

这些应该避免重复执行已检查的代码...

事实上,您可以使用附加的冒泡,而不是让多个复选框处理同一个单击事件处理程序名为 Button.Click 的路由事件。

  1. 处理按钮。在您的 DataGrid 级别单击。

    
            ....
    
  2. 在模板列中为您的复选框命名。

    
    
  3. 在 CheckBoxClick 处理程序中使用此代码...

    private void CheckBoxClick(object sender, RoutedEventArgs e)
    {
            var checkBox = e.OriginalSource as CheckBox;
            if (checkBox != null && checkBox.Name == "LocalColumnCheckbox")
            {
                    if (checkBox.IsChecked.GetValueOrDefault(false))
                    {
                               //// 您在此处检查的代码...
                    }
                    别的
                    {
                              //// 这里未经检查的代码...
                    }
            }
    }
    

让我知道这是否有帮助。

Checkbox's Checked / Unchecked events are actually primitive ToggleButton.Checked / Unchecked types and their usage is not encouraged by the developer community.

Instead you can handle CheckBox.Click event and inside that handler ....

 if (((CheckBox)sender).IsChecked.GetValueOrDefault(false))
 {
       //// Your Checked Code Here ...
 }
 else
 {
      //// Your unchecked code here ...
 }

These should avoid the repeated execution of the checked code...

In fact instead of having multiple checkboxes handling to the same click event handler you can use the bubbling of attached routed event called Button.Click.

  1. Handle Button.Click at your DataGrid level.

    <dg:DataGrid Button.Click="CheckBoxClick" ... >
            ....
    
  2. Name your checkbox in the template column.

    <CheckBox IsChecked="{Binding Selecting, UpdateSourceTrigger=PropertyChanged}" x:Name="LocalColumnCheckbox" />
    
  3. In the CheckBoxClick handler use this code ...

    private void CheckBoxClick(object sender, RoutedEventArgs e)
    {
            var checkBox = e.OriginalSource as CheckBox;
            if (checkBox != null && checkBox.Name == "LocalColumnCheckbox")
            {
                    if (checkBox.IsChecked.GetValueOrDefault(false))
                    {
                               //// Your Checked Code Here ...
                    }
                    else
                    {
                              //// Your unchecked code here ...
                    }
            }
    }
    

let me know if this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文