微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AVALONIA:将按钮绑定到在 Datagrid 中添加信息的方法

如何解决AVALONIA:将按钮绑定到在 Datagrid 中添加信息的方法

因此,我在 DataGrid 中创建了一个 Button 控件,用于向 DataGrid 添加新人信息。 像这样:

`<StackPanel>
    <Button Content="Add New Person" Margin="5" Command="{Binding AddNewPerson}"/>
    <DataGrid Items="{Binding People}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name" 
                                Binding="{Binding FirstName}" 
                                Width="2*" />
            <DataGridTextColumn Header="Last Name" 
                                Binding="{Binding LastName}" 
                                Width="2*" />
            <DataGridTextColumn Header="Department" 
                                Binding="{Binding DepartmentNumber}" 
                                Width="*" />
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>`

现在,当我尝试编写函数来绑定按钮时,以下代码段:

 public ReactiveCommand AddNewPerson { get; }

    public MainWindowviewmodel()
    {
        People = new ObservableCollection<Person>(GenerateMockPeopleTable());
        AddNewPerson = ReactiveCommand.Create(() =>
        {
            People.Add(
                new Person()
                {
                    FirstName = "New First Name",LastName = "New Last Name"
                });
        });

我收到以下错误

1. 'ReactiveCommand':static types cannot be used as return types.
2.Cannot Implicitly convert type 'ReactiveUI.ReactiveCommand<System.Reactive.Unit,System.Reactive.Unit> to .ReactiveUI.ReactiveCommand'

如果有人可以提供一些指导,那真的很有帮助!

解决方法

您的属性 public ReactiveCommand AddNewPerson { get; } 没有有效类型 - 您不能创建非通用的 ReactiveCommand,只能创建 ReactiveCommand<TInput,TOutput>

将其更改为 public ReactiveCommand<Unit,Unit> AddNewPerson { get; } 即可。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。