重复每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; }
}
答案 0 :(得分:0)
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的附加路由事件的冒泡来代替处理同一个Click事件处理程序的多个复选框。
处理按钮。单击您的DataGrid级别。
<dg:DataGrid Button.Click="CheckBoxClick" ... >
....
在模板列中为您的复选框命名。
<CheckBox IsChecked="{Binding Selecting, UpdateSourceTrigger=PropertyChanged}" x:Name="LocalColumnCheckbox" />
在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))
{
//// Your Checked Code Here ...
}
else
{
//// Your unchecked code here ...
}
}
}
请告诉我这是否有帮助。