使用下面的方法,我試圖為回圈遍歷的 DataGrid CheckBoxColumn 中的每一行觸發代碼。有用!什么不起作用,如果所有框都未選中,我還想觸發另一組代碼。現在,無論未選中多少個框,它都會觸發代碼,除非我選中第一個。我嘗試使用布爾標志跳出回圈
如何重新撰寫我的方法以達到預期的結果?我CheckBoxColumn的不是三態,我沒有研究過,我寧愿不,除非絕對必要。
public void VerifyInvoices() {
foreach (PaidTrip item in PaidTrips) {
if (item.IsChecked == true) {
ShowPreviewInvoiceDetailed();
First = false;
} else if (item.IsChecked == false && First == true) {
MessageBox.Show("You must choose an invoice to preview first.");
return;
}
}
}
我的CheckBoxColumn:
<DataGridCheckBoxColumn
Width="146" HeaderStyle="{StaticResource CenterGridHeaderStyle}"
Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="ClickMode" Value="Press"/>
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
uj5u.com熱心網友回復:
僅在驗證所有 PaidTrips 并且僅在未找到已檢查時才顯示 MessageBox:
public void VerifyInvoices()
{
bool success = false;
foreach (PaidTrip item in PaidTrips)
{
if (item.IsChecked == true)
{
ShowPreviewInvoiceDetailed();
success = true;
}
}
if (!success)
{
MessageBox.Show("You must choose an invoice to preview first.");
}
}
uj5u.com熱心網友回復:
您可以使用 LINQ 檢查所有專案是否滿足條件:
...
if (item.IsChecked == true) {
ShowPreviewInvoiceDetailed();
First = false;
} else
{
if(PaidTrips.All(t => t.IsChecked == false))
{
MessageBox.Show("You must choose an invoice to preview first.");
return;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411441.html
標籤:
上一篇:每當條件失敗時,我都無法回圈我的代碼。如果用戶輸入為真,它只會列印兩個代碼塊
下一篇:將序串列示為2個平方和
