【发布时间】:2019-01-16 05:54:11
【问题描述】:
我的模型、视图模型和XAML如下:
这是我的 ViewModelClass:
class AllResultsViewModel
{
private ICommand _clickCommand;
public ICommand ClickCommand
{
get
{
return _clickCommand ?? (_clickCommand = new CommandHandler(param => this.MyAction(_cvm),
param => this._canExecute));
}
}
private bool _canExecute;
private ComboBoxViewModel _cvm;
public DataTable AllResults { get; set; }
public AllResultsViewModel(ComboBoxViewModel CVM)
{
_canExecute = true;
_cvm = CVM;
}
public void MyAction(ComboBoxViewModel cvm)
{
//Connecting to DB to retrieve data in datatable
}
}
public class CommandHandler : ICommand
{
private Action<object> _execute;
// private bool _canExecute;
private Predicate<object> _canExecute;
#region Constructors
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public CommandHandler(Action<object> execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public CommandHandler(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
[DebuggerStepThrough]
public bool CanExecute(object parameters)
{
// return _canExecute;
return _canExecute == null ? true : _canExecute(parameters);
}
// public event EventHandler CanExecuteChanged;
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameters)
{
_execute(parameters);
}
}
我的 XAML 如下:
<DataGrid Name="results_grid" IsReadOnly="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Margin="10" ItemsSource="{Binding AllResults}" DisplayMemberPath="AllResultsGrid" ColumnWidth="100" RowHeight="30">
我的模型类:
公共类 AllResultsModel { 私有数据表_allresultsgrid;
public DataTable AllResultsGrid
{
get { return _allresultsgrid; }
set { _allresultsgrid = value; }
}
}
我在这里遗漏了什么吗?代码构建成功,数据从数据库中检索。但我无法在 Datagrid 中查看它。
【问题讨论】: