[英]How can I for each row in DataGrid generate checkbox or whatever just to select a multiple rows?
I'm working on small WPF app, I'm fetching data from a database and it looks like this: 我正在使用小型WPF应用程序,正在从数据库中获取数据,它看起来像这样:
public List<BillItemInSerie> GetSerialNumbers(BillItemsTemp stavka)
{
List<BillItemInSerie> serialNumbers = new List<BillItemInSerie>();
//serialNumbers = Controller.GetSerialFromDatabase(); // this is currently not working because I don't have any data in db
for(int i = 0;i<10;i++)
{
BillItemInSerie serialNumber = new BillItemInSerie();
serialNumber.ArticleId = i;
serialNumber.ExpireDate = DateTime.Now;
serialNumber.Lot = "Warehouse" + " " + i;
serialNumber.Serial = "135" + DateTime.Now.Minute.ToString() + "/x";
serialNumbers.Add(serialNumber);
}
dtgSerialNumbers.ItemsSource = serialNumbers;
return serialNumbers;
}
As you can see I'm actually not getting it from a db because I don't have any rows in my tables, so I created by myself 10 objects to work with. 如您所见,由于我的表中没有任何行,因此我实际上没有从数据库获取它,所以我自己创建了10个要使用的对象。
Here is my XAML: 这是我的XAML:
<DataGrid Name="dtgSerialNumbers" SelectionUnit="FullRow" EnableColumnVirtualization = "True" EnableRowVirtualization ="True" MaxWidth="4000" MaxHeight="2000" Background="White" Margin="5,5,5,0" AutoGenerateColumns="False" RowHeaderWidth="0" HorizontalGridLinesBrush="#0091EA" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" RowHeight="35" Grid.ColumnSpan="2" Grid.Row="2">
<DataGrid.CellStyle>
<StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Black"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{x:Static local:Globals.dataGridfontSizeHeader}"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="40"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="LightBlue"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Serial}" Header="Serial" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="20*" />
<DataGridTextColumn Binding="{Binding Lot}" Header="Desc." Foreground="Black" FontSize="15" FontFamily="Verdana" Width="40*" />
<DataGridTextColumn Binding="{Binding ExpireDate, StringFormat ={}{0:dd.MM.yyyy HH:mm:ss}}" FontSize="15" Header="Date" FontFamily="Verdana" Foreground="Black" Width="25*" />
<DataGridTextColumn Binding="{Binding IsSelected}" Header="Select" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="15*" />
</DataGrid.Columns>
</DataGrid>
Now I'm wondering how can I select corresponding ROWS, maybe somehow generate checkboxes next to each row and select a row by that? 现在我想知道如何选择相应的ROWS,也许以某种方式在每行旁边生成复选框并以此选择一行?
Thanks EDIT: 谢谢编辑:
After suggestions I've changed column to < DataGridCheckBoxColumn
> instead of DataGridTextColumn
and I've wrote this: 在提出建议之后,我将列更改为<
DataGridCheckBoxColumn
>而不是DataGridTextColumn
并且这样写:
foreach (BillItemInSerie item in dtgSerialNumbers.ItemsSource)
{
if (((CheckBox)colSelektiraj.GetCellContent(item)).IsChecked == true)
{
MessageBox.Show(item.Lot.ToString());
}
}
It's basically for each selected row get value.. 它基本上是为每个选定的行获取值。
I'm not sure if this is right approach but I think it works. 我不确定这是否正确,但我认为它可行。
You could set the SelectionMode
property of the DataGrid
to Extended
and add a DataGridTemplateColumn
: 您可以将
DataGrid
的SelectionMode
属性设置为Extended
并添加一个DataGridTemplateColumn
:
<DataGrid Name="dtgSerialNumbers" SelectionUnit="FullRow" SelectionMode="Extended" ...>
<DataGrid.Columns>
...
<DataGridTemplateColumn Header="Select">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}, Mode=TwoWay}" IsHitTestVisible="False" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Then you should be able to select several rows by pressing the CTRL and click. 然后,您应该能够通过按CTRL键并单击来选择几行。
please take a look on this post . 请看一下这篇文章 。 in my eyes just put a checkbox inside of your datagdrid an bind the dataset ID to a command parameter like in this example
在我眼中,只是在datagdrid内放置一个复选框,然后将数据集ID绑定到命令参数,如本例所示
<CheckBox CommandParameter="{Binding Path=Id}"
Command="{Binding DataContext.AddRemovePresetAssignmentCommand,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}"
Content="{Binding Path=Name}"
>
then you have all possibilities in your viewmodel 那么你在视图模型中拥有了所有的可能性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.