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; }
}
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.