第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將方法或事件綁定到 WPF 中 ListViewItem 模板中的按鈕?

如何將方法或事件綁定到 WPF 中 ListViewItem 模板中的按鈕?

C#
守著一只汪 2022-06-18 17:17:27
我想將模型的方法綁定到 ListViewItem 模板中的按鈕控件。如何在 WPF 中做到這一點?我試過了,但在運行時出現(xiàn)錯誤。錯誤是...我只需要一種非常簡單且正確的方法來將方法綁定到按鈕單擊事件。請幫幫我。我是 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" />        
查看完整描述

2 回答

?
明月笑刀無情

TA貢獻1828條經(jīng)驗 獲得超4個贊

如果您使用數(shù)據(jù)綁定,請不要處理Click事件。使用Button.Command,并將其綁定到視圖模型的ICommand屬性:


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:


<Button Content="Click me!" Command="{Binding SomeCommand}"/>


查看完整回答
反對 回復 2022-06-18
?
夢里花落0921

TA貢獻1772條經(jīng)驗 獲得超6個贊

您不能在 MVVM 中綁定方法。您需要改用命令。命令由控件的默認動作行為觸發(fā)(例如,對于 Button,默認觸發(fā)是 Click)(如果您想將命令綁定到默認之外的其他事件,則需要編寫一些交互邏輯或使用支持它的庫)。


這是命令的示例:


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);

}

然后在按鈕上設(shè)置 Command 屬性:Command="{Binding ShowEntitiesCommmand}" CommandParameter="{Binding SomeParameterYoudLikeToPass}"


在這里您可以查看實現(xiàn) ICommand 的 RelayCommand 類的示例: https ://github.com/Nidrax/Veritaware.Toolkits.LightVM/blob/master/Veritaware.Toolkits.LightVM.Net/RelayCommand.cs


查看完整回答
反對 回復 2022-06-18
  • 2 回答
  • 0 關(guān)注
  • 437 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號