我在wpf中有一个datagrid,我在该datagrid中有多行(items),每行有一个复选框列。我想检查所有行,如果在任何行中选中了复选框,则执行下面的操作是我的代码。谢谢!
WPF代码
<DataGrid CanUserAddRows="False" AutoGenerateColumns="False"
CellEditEnding="SaveDeliveryValue" LoadingRow="DataGrid_LoadingRow"
Name="ViewOrdersGrid" HorizontalAlignment="Center" Margin="0,10,0,0"
VerticalAlignment="Top" Height="278" Width="520" BorderBrush="#FFA0A0A0">
<DataGrid.Columns>
<DataGridTextColumn Header="Order No" Width="115" Binding="{Binding Path=BONo, Mode=OneWay}" />
<DataGridTextColumn Header="Order Date" Width="100" Binding="{Binding Path=BODate, Mode=OneWay, StringFormat=d}" />
<DataGridTextColumn Header="Total Amount" Width="100" Binding="{Binding Path=BOTotal, Mode=OneWay}" />
<DataGridTextColumn Header="Total Bikes" Width="100" Binding="{Binding Path=BOTatalBikes, Mode=OneWay}" />
<DataGridCheckBoxColumn Header="Delivered" x:Name="DeliveryValueCheck" Width="70" Binding="{Binding Path=BODelivered, Mode=TwoWay}" />
</DataGrid.Columns>
</DataGrid>
C#代码
private void Window_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 0; i < ViewOrdersGrid.Items.Count; i++)
{
CheckBox mycheckbox = ViewOrdersGrid.Columns[4].GetCellContent(ViewOrdersGrid.Items[i]) as CheckBox;
if (mycheckbox.IsChecked == true)
{
MessageBox.Show("Checked");
}
}
}
答案 0 :(得分:1)
你已经在使用MVVM,我可以通过绑定看到,所以你有一个良好的开端。现在,因为MVVM允许UI和数据之间的关系非常紧密,我们可以推断如果我们可以遍历给定对象上的checked属性的可视树,我们也应该能够遍历这样的属性的数据。所以你的C#代码应该如下所示(在你的代码中假设DataGrid的ItemsSource绑定到一个集合(让我们称之为MyItems):
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var viewModel = (ViewModelType)this.DataContext;
foreach(var item in viewModel.MyItems)
{
if(item.BODelivered)
{
MessageBox.Show("Checked");
}
}
}
此示例假定(因为您的示例的其余部分正确使用绑定)您的网格绑定到某些东西(我们称之为MyItems)。如果您需要了解其工作原理(意味着您尚未将其实现为MVVM和 FOOLED ME ),请考虑以下事项:
这是你的XAML
<DataGrid CanUserAddRows="False" AutoGenerateColumns="False"
CellEditEnding="SaveDeliveryValue" LoadingRow="DataGrid_LoadingRow"
Name="ViewOrdersGrid" HorizontalAlignment="Center" Margin="0,10,0,0"
VerticalAlignment="Top" Height="278" Width="520" BorderBrush="#FFA0A0A0"
ItemsSource="{Binding MyItems}">
<DataGrid.Columns>
<DataGridTextColumn Header="Order No" Width="115" Binding="{Binding Path=BONo, Mode=OneWay}" />
<DataGridTextColumn Header="Order Date" Width="100" Binding="{Binding Path=BODate, Mode=OneWay, StringFormat=d}" />
<DataGridTextColumn Header="Total Amount" Width="100" Binding="{Binding Path=BOTotal, Mode=OneWay}" />
<DataGridTextColumn Header="Total Bikes" Width="100" Binding="{Binding Path=BOTatalBikes, Mode=OneWay}" />
<DataGridCheckBoxColumn Header="Delivered" x:Name="DeliveryValueCheck" Width="70" Binding="{Binding Path=BODelivered, Mode=TwoWay}" />
</DataGrid.Columns>
</DataGrid>
这是您的数据结构
public class MyObject
{
public int BONo { get; set; }
public DateTime BODate { get; set; }
public int BOTotal { get; set; }
public int BOTatalBikes { get; set; }
public bool BODelivered { get; set; }
}
这是您的* .xaml.cs文件
// this is the constructor for your view (MyWindow.xaml.cs)
private MyWindow( )
{
this.DataContext = new MyViewModel( );
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var viewModel = (ViewModelType)this.DataContext;
foreach(var item in viewModel.MyItems)
{
if(item.BODelivered)
{
MessageBox.Show("Checked");
}
}
}
这是您的视图模型(MyViewModel.cs)
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void OnPropertyChanged(string property)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
private ObservableCollection<YourObjectTypeHere> _myItems;
public ObservableCollection<YourObjectTypeHere> MyItems
{
get
{
return _myItems;
}
set
{
_myItems = value;
OnPropertyChanged("MyItems");
}
}
}