簡體   English   中英

帶有MVVM和CommandParameter的ListBox SelectionChanged事件

[英]ListBox SelectionChanged event with MVVM and CommandParameter

我正在使用WPF,C#和.NET Framework 4.6.1開發MVVM應用程序。

我正在嘗試使用MVVM實現ListBox SelectionChanged事件。 為此,我這樣做:

安裝nuget軟件包: PM> Install-Package System.Windows.Interactivity.WPF
在xaml上添加xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
將此代碼添加到xaml中的ListBox中:

<ListBox x:Name="listBoxTrzType" Grid.Column="1" Margin="10,0,25,0" VerticalAlignment="Center" Height="25" ItemsSource="{Binding TrzTypes}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <interactivity:Interaction.Triggers>
        <interactivity:EventTrigger EventName="SelectionChanged">
            <interactivity:InvokeCommandAction Command="{Binding ListBoxTrzTypeSelectionChanged}"
                                                CommandParameter="{Binding ElementName=listBoxTrzType, Path=SelectedItem}">
            </interactivity:InvokeCommandAction>
        </interactivity:EventTrigger>
    </interactivity:Interaction.Triggers>
</ListBox>

在ViewModel上,我有:

public ICommand ListBoxTrzTypeSelectionChanged
{
    get { return new DelegateCommand(TrzTypeSelectionChanged); }
}

private void TrzTypeSelectionChanged()
{
    throw new NotImplementedException();
}

DelegateCommand類:

public class DelegateCommand : ICommand
{
    private readonly Action _action;

    public DelegateCommand(Action action)
    {
        _action = action;
    }

    public void Execute(object parameter)
    {
        _action();
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

#pragma warning disable 67
    public event EventHandler CanExecuteChanged { add { } remove { } }
#pragma warning restore 67
}

但是我的問題是我不知道如何將CommandParameter="{Binding ElementName=listBoxTrzType, Path=SelectedItem}"傳遞給private void TrzTypeSelectionChanged()

我在Internet上進行了很多搜索,但是沒有找到任何示例(僅包含CanExecute示例)。

我必須如何修改ViewModel類才能訪問SelectedItem參數?

實際上,將交互行為用於此類任務實在是太過分了。
您甚至在這里都不需要命令。 SelectedItem屬性添加到視圖模型並偵聽屬性更改就足夠了:

public class SomeVm : INotifyPropertyChanged
{
    // INPC implementation is omitted

    public IEnumerable<SomeType> Items { get; }
    public SomeType SelectedItem
    {
        get { return selectedItem; }
        set
        {
            if (selectedItem != value)
            {
                selectedItem = value;
                OnPropertyChanged();
                // do what you want, when property changes
            }
        }
    }
}

XAML:

<ListBox ItemsSource="{Binding Items}"
         SelectedItem="{Binding SelectedItem}"
         DisplayMemberPath="Name"/>

在您的DelegateCommand
更改private readonly Action _action; private readonly Action<object> _action; 然后在您的execute方法中

public void Execute(object parameter)
{
    _action.Invoke(parameter);
}  

現在,您的ViewModel函數需要如下所示:

private void TrzTypeSelectionChanged(object parameter)
{
    throw new NotImplementedException();
}  

而且你很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM