I use Syncfusion in my WPF project. The following is a DataGrid with multiple selection modes:
<grid:SfDataGrid x:Name="dataGrid"
SelectionMode="Multiple"
Grid.Row="0"
SelectedItems="{Binding SelectedRows}"
BorderThickness="1"
NavigationMode="Row"
BorderBrush="Black"
CellStyle="{StaticResource ResourceKey=cellStyle}"
AutoGenerateColumns="False"
HeaderStyle="{StaticResource ResourceKey=headerStyle}"
ColumnSizer="AutoLastColumnFill"
ItemsSource="{Binding PagedSource, ElementName=dataPager}">
<grid:SfDataGrid.Columns>
<grid:GridCheckBoxSelectorColumn MappingName="SelectorColumn"
Width="30" />
<grid:GridTextColumn AllowDragging="False"
HeaderText="Id"
MappingName="EquipmentId"
IsHidden="True" />
<grid:GridTextColumn AllowDragging="False"
HeaderText="Code"
MappingName="EquipmentCode" />
<grid:GridTextColumn AllowDragging="False"
HeaderText="Title"
MappingName="EquipmentTitle" />
</grid:SfDataGrid.Columns>
</grid:SfDataGrid>
I want to get a list of selected rows (their Ids that exist in the first hidden column) in the ViewModel. How can I achieve this?
Note: I use Microsoft.Toolkit.MVVM in my project.
CodePudding user response:
i dont know if it helps but i know how to only get the selected item by using this in xaml side
<grid:SfDataGrid x:Name="dataGrid" SelectionChanged="GetSelected">
this is in c# side
private void GetSelected(object sender, SelectionChangedEventArgs e)
{
Object selectedItem = dataGrid.SelectedItem as Object;
}
CodePudding user response:
I used SelectedItems="{Binding SelectedItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
in the XAML code and then, I used the following property in the ViewModel:
public class EquipmentIdentitySettingViewModel: ObservableObject {
//...Some codes here
private ObservableCollection < object > _selectedItems;
public ObservableCollection < object > SelectedItems {
get {
return _selectedItems;
}
set {
SetProperty(ref _selectedItems, value);
}
}
public ICommand ApplyCommand {
get;
}
public EquipmentIdentitySettingViewModel() {
ApplyCommand = new RelayCommand(() => ApplyChanges());
}
private void ApplyChanges() {
if (SelectedItems != null) {
foreach(Equipment item in SelectedItems) {
MessageBox.Show(item.EquipmentCode.ToString());
}
}
}
}
The problem solved.