Using Prism framework to implement a WPF MVVM solution.
I want to display tabular data, either using DataGrid or GridView. Then when clicking in a cell, it should fire off a Command and as the CommandParameter, the ColumnHeader's caption. (Only for the Debit & Credit columns as seen in code below. What the code eventually will do is when clicking in the Debit field, the Amount will copied to that field. If then clicked in the Credit field, the Debit field will be cleared and the value copied into the Credit field)
Tried DataGrid, and only manage to pass the selected record back to the ViewModel. Using GridView, only manage to attach a command to the GridViewColumn header.
Any help will be appreciated.
At the moment, this is what my code looks like:
ViewModel:
public AccountingViewModel(IAccountingView view, IRegionManager manager, IEventAggregator eventAggregator)
:base(view, manager, eventAggregator)
{
AccountingEntries = new ObservableCollection<AccountingModel>();
FieldClickedCommand = new DelegateCommand<string>(OnClicked);
}
private void OnClicked(string column)
{
//Update corresponding field in record clicked cell
}
private AccountingModel _selectedAccount;
public AccountingModel SelectedAccount
{
get { return _selectedAccount; }
set
{
_selectedAccount = value;
OnPropertyChanged("SelectedAccount");
}
}
private ObservableCollection<AccountingModel> _accountingEntries;
public ObservableCollection<AccountingModel> AccountingEntries
{
get { return _accountingEntries; }
set
{
_accountingEntries = value;
OnPropertyChanged("AccountingEntries");
}
}
View:
<Grid x:Name="AccountingViewModelLevel">
<DataGrid Grid.Row="1"
ItemsSource="{Binding AccountingEntries}"
SelectedItem="{Binding SelectedAccount}"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserResizeColumns="False"
CanUserReorderColumns="False"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
SelectionMode="Single"
x:Name="AccountEntryDataGrid">
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
Command="{Binding FieldClickedCommand}"
CommandParameter="{Binding ElementName=AccountEntryDataGrid, Path=SelectedItem}" />
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ID}" Header="No."/>
<DataGridTextColumn Binding="{Binding AccountName}" Header="Account Name"/>
<DataGridTextColumn Binding="{Binding Amount, StringFormat=0.00;;#}"
Header="Amount"
Width="90" />
<DataGridTextColumn Binding="{Binding Debit, StringFormat=0.00;;#}"
Header="Debit"
Width="90"
CellStyle="{StaticResource TrialBalanceStyle}"
>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Credit, StringFormat=0.00;;#}"
Header="Credit"
Width="90"
CellStyle="{StaticResource TrialBalanceStyle}"
IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>