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

我想将模型的方法绑定到 ListViewItem 模板中的按钮控件。如何在 WPF 中做到这一点?我试过了,但在运行时出现错误。

错误是...

http://img4.mukewang.com/62ad983b0001472913640766.jpg

我只需要一种非常简单且正确的方法来将方法绑定到按钮单击事件。请帮帮我。我是 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" />

        

守着一只汪
浏览 407回答 2
2回答

明月笑刀无情

如果您使用数据绑定,请不要处理Click事件。使用Button.Command,并将其绑定到视图模型的ICommand属性:public class SomeVm{&nbsp; &nbsp; public SomeVm()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // initialize SomeCommand here;&nbsp; &nbsp; &nbsp; &nbsp; // usually you need RelayCommand/DelegateCommand&nbsp; &nbsp; &nbsp; &nbsp; SomeCommand = new RelayCommand(SomeMethod);&nbsp; &nbsp; }&nbsp; &nbsp; public ICommand SomeCommand { get; }&nbsp; &nbsp; private void SomeMethod()&nbsp; &nbsp; {&nbsp; &nbsp; }}XAML:<Button Content="Click me!" Command="{Binding SomeCommand}"/>

梦里花落0921

您不能在 MVVM 中绑定方法。您需要改用命令。命令由控件的默认动作行为触发(例如,对于 Button,默认触发是 Click)(如果您想将命令绑定到默认之外的其他事件,则需要编写一些交互逻辑或使用支持它的库)。这是命令的示例:private ICommand _ShowEntitiesCommand;public ICommand ShowEntitiesCommmand{&nbsp; &nbsp; get&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (_ShowEntitiesCommand == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ShowEntitiesCommand = new RelayCommand(ShowEntities);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return _ShowEntitiesCommand;&nbsp; &nbsp; }}private void ShowEntities(object parameter){&nbsp; &nbsp; SelectedViewModel = viewModelLocator.Get(parameter);}然后在按钮上设置 Command 属性:Command="{Binding ShowEntitiesCommmand}" CommandParameter="{Binding SomeParameterYoudLikeToPass}"在这里您可以查看实现 ICommand 的 RelayCommand 类的示例: https ://github.com/Nidrax/Veritaware.Toolkits.LightVM/blob/master/Veritaware.Toolkits.LightVM.Net/RelayCommand.cs
打开App,查看更多内容
随时随地看视频慕课网APP