使用Prism框架实现WPF MVVM解决方案。
我想使用DataGrid或GridView显示表格数据。 然后当单击一个单元格时,它应该触发一个Command,并作为CommandParameter,即ColumnHeader的标题。 (仅适用于Debit& Credit列,如下面的代码所示。代码最终将执行的操作是单击Debit字段时,Amount将复制到该字段。如果随后单击Credit字段,Debit字段将为清除并将值复制到Credit字段中
尝试使用DataGrid,并且只设法将所选记录传递回ViewModel。 使用GridView,只能管理将命令附加到GridViewColumn标题。
任何帮助将不胜感激。
目前,这就是我的代码:
视图模型:
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");
}
}
查看:
<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>