C# 如何将方法或事件绑定到WPF中ListViewItem模板中的按钮?

C# 如何将方法或事件绑定到WPF中ListViewItem模板中的按钮?,c#,wpf,C#,Wpf,我想将模型的方法绑定到ListViewItem模板中的按钮控件。我怎样才能在WPF中做到这一点?我尝试过,但在运行时出错 错误是。。。 我只需要一个非常简单和正确的方法来绑定一个按钮点击事件的方法。 请帮帮我。我是WPF的新手 这是我的代码 <ListView Grid.Row="1" MaxHeight="500" Name="entryLisView" Grid.Column

我想将模型的方法绑定到ListViewItem模板中的按钮控件。我怎样才能在WPF中做到这一点?我尝试过,但在运行时出错

错误是。。。

我只需要一个非常简单和正确的方法来绑定一个按钮点击事件的方法。 请帮帮我。我是WPF的新手

这是我的代码

 <ListView Grid.Row="1"
                  MaxHeight="500"
                  Name="entryLisView"
                  Grid.ColumnSpan="2">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Background="White">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="New"
                                   Margin="10,5,0,5"
                                   VerticalAlignment="Top"
                                   HorizontalAlignment="Left" />
                        <Button Name="cancelThisBtn"
                                Content="Delete"
                                Visibility="{Binding ItemCount, Mode=OneWay, Converter={StaticResource EduListItemCancelBtnVisibilityConverter}}"
                                Grid.Column="1"
                                Margin="0,5,10,5"
                                Click="{Binding DeleteDegree}"
                                VerticalAlignment="Top"
                                HorizontalAlignment="Right" />
                        <TextBox wpf:HintAssist.Hint="Exam or Degree Title"
                                 Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
                                 Width="230"
                                 FontSize="18"
                                 Grid.Row="1"
                                 Margin="10,0,10,0"
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Center"
                                 Text="{Binding Title, Mode=TwoWay}" />

                        <TextBox wpf:HintAssist.Hint="Subject"
                                 Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
                                 Width="230"
                                 FontSize="18"
                                 Margin="10,0,10,0"
                                 Grid.Row="1"
                                 Grid.Column="1"
                                 Text="{Binding SubjectName, Mode=TwoWay}"
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Center" />

                        <TextBox wpf:HintAssist.Hint="Institute Name"
                                 Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
                                 Width="230"
                                 FontSize="18"
                                 Margin="10,20,10,0"
                                 Grid.Row="2"
                                 Grid.Column="0"
                                 Text="{Binding InstituteName, Mode=TwoWay}"
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Center" />

                        <TextBox wpf:HintAssist.Hint="Passing Year"
                                 Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
                                 Width="230"
                                 FontSize="18"
                                 Margin="10,20,10,0"
                                 Grid.Row="2"
                                 Grid.Column="1"
                                 Text="{Binding PassingYear, Mode=TwoWay}"
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Center" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

如果使用数据绑定,请不要处理
单击事件。使用
按钮。命令
,并将其绑定到查看模型的属性:

public class SomeVm
{
    public SomeVm()
    {
        // initialize SomeCommand here;
        // usually you need RelayCommand/DelegateCommand
        SomeCommand = new RelayCommand(SomeMethod);
    }

    public ICommand SomeCommand { get; }

    private void SomeMethod()
    {
    }
}
XAML:


如果使用数据绑定,请不要处理
单击事件。使用
按钮。命令
,并将其绑定到查看模型的属性:

public class SomeVm
{
    public SomeVm()
    {
        // initialize SomeCommand here;
        // usually you need RelayCommand/DelegateCommand
        SomeCommand = new RelayCommand(SomeMethod);
    }

    public ICommand SomeCommand { get; }

    private void SomeMethod()
    {
    }
}
XAML:


您不能在MVVM中绑定方法。您需要改用命令。命令由控件的默认操作行为触发(例如,对于按钮,默认触发器是单击)(如果要将命令绑定到默认以外的其他事件,则需要编写一些交互逻辑或使用支持它的库)

以下是该命令的一个示例:

private ICommand _ShowEntitiesCommand;

public ICommand ShowEntitiesCommmand
{
    get
    {
        if (_ShowEntitiesCommand == null)
        {
            _ShowEntitiesCommand = new RelayCommand(ShowEntities);
        }

        return _ShowEntitiesCommand;
    }
}

private void ShowEntities(object parameter)
{
    SelectedViewModel = viewModelLocator.Get(parameter);
}
然后在按钮上设置Command属性:Command=“{Binding showenties commmand}”CommandParameter=“{Binding someparameter youdliketopass}”

在这里,您可以查看实现ICommand的RelayCommand类的示例:
您不能在MVVM中绑定方法。您需要改用命令。命令由控件的默认操作行为触发(例如,对于按钮,默认触发器是单击)(如果要将命令绑定到默认以外的其他事件,则需要编写一些交互逻辑或使用支持它的库)

以下是该命令的一个示例:

private ICommand _ShowEntitiesCommand;

public ICommand ShowEntitiesCommmand
{
    get
    {
        if (_ShowEntitiesCommand == null)
        {
            _ShowEntitiesCommand = new RelayCommand(ShowEntities);
        }

        return _ShowEntitiesCommand;
    }
}

private void ShowEntities(object parameter)
{
    SelectedViewModel = viewModelLocator.Get(parameter);
}
然后在按钮上设置Command属性:Command=“{Binding showenties commmand}”CommandParameter=“{Binding someparameter youdliketopass}”

在这里,您可以查看实现ICommand的RelayCommand类的示例:

看起来它试图告诉您您的xaml格式不正确(在错误消息中的位置详细信息)。因此
第59行,位置33
?看起来它试图告诉您您的xaml格式不正确(在错误消息中的位置详细信息)。那么在任何items控件模板中
第59行,位置33
,该命令通常是主视图模型的属性,而不是单个数据项,因此您需要使用相对绑定-例如,在任何项目控制模板中,该命令通常是主视图模型的属性,而不是单个数据项,因此您需要使用相对绑定-例如。