我有一个数据网格,它绑定到包含itemsource的项目列表。复选框控件是网格上的一列,如果选中,则会在单击按钮时存储特定信息。我的问题是我无法将对象checkBox
绑定到XAML复选框,当我检查checkbox
它给我一个空值或假。
格式框的XAML:
<DataGridTemplateColumn Header="Ist aktiv">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path= checkBox }" Name="checkBox" IsEnabled="{Binding Enabled}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn
>
按钮事件:
private void checkinCheckBox() {
List<AwaitingMeds> awaitingMedslist = MedicationDatagrid.ItemsSource as List<AwaitingMeds>;
//List<AwaitingMeds> awaitingMedslistSelected = new List<AwaitingMeds>();
List<AwaitingMeds> awaitingMedslistSelected = ( from m in awaitingMedslist
where m.checkBox.Equals( true )
select m ).ToList<AwaitingMeds>();
foreach( AwaitingMeds checkedItems in awaitingMedslistSelected )
{
MedicationDispenseData.UpdateMedicationDispense( checkedItems.MedicationDispenseID, ( int )Enumerations.MedicationDispenseStatus.CheckedIn,false, "" );
MedicationDispenseHistory medicationDispenseHistory = new MedicationDispenseHistory();
medicationDispenseHistory.MedicationDispenseID = checkedItems.MedicationDispenseID;
medicationDispenseHistory.StaffID = GlobalVariables.loggedInStaff.StaffID;
medicationDispenseHistory.MedicationDispenseStatusID = ( int )Enumerations.MedicationDispenseStatus.CheckedIn;
MedicationDispenseHistoryData.Create( medicationDispenseHistory );
}
}
public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid )
{
var itemsSource = grid.ItemsSource as System.Collections.IEnumerable;
if( null == itemsSource ) yield return null;
foreach( var item in itemsSource )
{
var row = grid.ItemContainerGenerator.ContainerFromItem( item ) as DataGridRow;
if( null != row ) yield return row;
}
}
答案 0 :(得分:0)
您的复选框被绑定为已启用,因此您应该检查m.Enabled而不是m.checkbox
List<AwaitingMeds> awaitingMedslistSelected = ( from m in awaitingMedslist
where m.**Enabled**.Equals( true )
select m ).ToList<AwaitingMeds>();
答案 1 :(得分:0)
我遇到了同样的问题。
您无法按名称访问DataTemplate
中的控件。
想象一下如果你的DataGrid
有1000行会发生什么!
将有1000个“checkbox
”es !!!!!
所以试试:
<CheckBox IsChecked="{Binding PropertyName,RelativeSource={RelativeSource Self}}" />
注意:您的命名非常容易引起误解,因此我可能会误解您的喜好。如果是,请发表评论并编辑问题!