关于datagrid中CheckBox的问题
重复每个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Checkbox 的 Checked / Unchecked 事件实际上是原始的 ToggleButton.Checked / Unchecked 类型,开发人员社区不鼓励使用它们。
相反,您可以处理 CheckBox.Click 事件并在该处理程序内部...。
这些应该避免重复执行已检查的代码...
事实上,您可以使用附加的冒泡,而不是让多个复选框处理同一个单击事件处理程序名为 Button.Click 的路由事件。
处理按钮。在您的 DataGrid 级别单击。
在模板列中为您的复选框命名。
在 CheckBoxClick 处理程序中使用此代码...
让我知道这是否有帮助。
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 ....
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.
Handle Button.Click at your DataGrid level.
Name your checkbox in the template column.
In the CheckBoxClick handler use this code ...
let me know if this helps.