ICommand 在数据网格单元模板中不起作用
这是我的 MainPage.xaml :-
<sdk:DataGrid Margin="17,17,20,76" AutoGenerateColumns="False" ItemsSource="{Binding Students}">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding StudName}" Header="Student Name">
</sdk:DataGridTextColumn>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button CommandParameter="{Binding}" Command="{Binding Path=DataContext.AddCommand,ElementName=root}"
Content="Add Student" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
在后面的代码中,我已将数据上下文设置为 viewmodel 实例。
This is my viewmodel :-
using SampleApp.Misc;
using SampleApp.Model;
using SampleApp.Web;
using System.Collections.ObjectModel;
using SampleApp.Commands;
namespace SampleApp.VM
{
public class MainPageViewModel : ViewModelBase
{
private StudentModel _model = new StudentModel();
public MainPageViewModel()
{
_model.GetStudentAsyncComplete += _model_GetStudentAsyncComplete;
_model.GetStudentAsync();
}
private RelayCommand<Student> _addCommand = null;
public RelayCommand<Student> AddCommand
{
get
{
if (_addCommand == null)
{
_addCommand = new RelayCommand<Student>(student =>
{
}, student => student != null);
}
return _addCommand;
}
}
private ObservableCollection<Student> _students;
public ObservableCollection<Student> Students
{
get { return _students; }
set
{
_students = value;
RaisePropertyChanged("Students");
}
}
void _model_GetStudentAsyncComplete(object sender, EntityResultArgs<Web.Student> e)
{
if (e.Error == null)
{
Students = new ObservableCollection<Student>(e.Results);
}
}
}
}
为什么我的 AddStudent 命令没有在 ViewModel 中触发?有什么想法吗?如果我将它放在 Datagrid 之外,它就可以正常工作。
This is my MainPage.xaml :-
<sdk:DataGrid Margin="17,17,20,76" AutoGenerateColumns="False" ItemsSource="{Binding Students}">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding StudName}" Header="Student Name">
</sdk:DataGridTextColumn>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button CommandParameter="{Binding}" Command="{Binding Path=DataContext.AddCommand,ElementName=root}"
Content="Add Student" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
In code behind I have set the datacontext to viewmodel instance.
This is my viewmodel :-
using SampleApp.Misc;
using SampleApp.Model;
using SampleApp.Web;
using System.Collections.ObjectModel;
using SampleApp.Commands;
namespace SampleApp.VM
{
public class MainPageViewModel : ViewModelBase
{
private StudentModel _model = new StudentModel();
public MainPageViewModel()
{
_model.GetStudentAsyncComplete += _model_GetStudentAsyncComplete;
_model.GetStudentAsync();
}
private RelayCommand<Student> _addCommand = null;
public RelayCommand<Student> AddCommand
{
get
{
if (_addCommand == null)
{
_addCommand = new RelayCommand<Student>(student =>
{
}, student => student != null);
}
return _addCommand;
}
}
private ObservableCollection<Student> _students;
public ObservableCollection<Student> Students
{
get { return _students; }
set
{
_students = value;
RaisePropertyChanged("Students");
}
}
void _model_GetStudentAsyncComplete(object sender, EntityResultArgs<Web.Student> e)
{
if (e.Error == null)
{
Students = new ObservableCollection<Student>(e.Results);
}
}
}
}
Why isn't my command of AddStudent firing in ViewModel? Any idea? If I place it outside Datagrid it works absolutely fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请查看这篇文章
您需要一个 DataContextProxy 来触发 DataGridCell 内的命令。 ElementBinding 将不起作用。
please take a look at this post
You need a DataContextProxy to fire Commands inside a DataGridCell. The ElementBinding won't work.