ICommand 在数据网格单元模板中不起作用

发布于 2024-11-07 20:02:24 字数 2570 浏览 0 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾 2024-11-14 20:02:24

请查看这篇文章

您需要一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文