首页 > 解决方案 > ListBox SelectedIndex 作为命令参数

问题描述

Command 参数如何绑定到 ListBox 中的选定索引?我的代码:

<ListBox>
        <i:Interaction.Triggers>
            <i:EventTrigger
                EventName="MouseDoubleClick">
                <i:InvokeCommandAction
                    Command="{
                              Binding Path=SelectPath,  
                              Mode=OneTime, 
                              RelativeSource={
                                            RelativeSource Mode=FindAncestor, 
                                            AncestorType={x:Type UserControl}
                                     }
                 }" 
                    CommandParameter="ListBox.SelectedIndex" // how can this parameter be bind to SelectedIndex
                    />
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </ListBox>

编辑:

我做了什么并且它有效:创建了新的 dp 属性 SelectedListBoxIndex 将其与 SelectedIndex 绑定,然后将属性作为命令参数传递。但是有没有更好的方法呢?

        <ListBox
        SelectedIndex="{
                    Binding Path=SelectedListBoxIndex,
                    RelativeSource={
                                    RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type UserControl}
                                   }}">

        <i:Interaction.Triggers>
            <i:EventTrigger
                EventName="MouseDoubleClick">
                <i:InvokeCommandAction
                    Command="{
                              Binding Path=SelectPath,  
                              Mode=OneTime, 
                              RelativeSource={
                                            RelativeSource Mode=FindAncestor, 
                                            AncestorType={x:Type UserControl}
                                     }
                 }"
                    CommandParameter="{
                    Binding Path=SelectedListBoxIndex,
                    RelativeSource={
                                    RelativeSource Mode=FindAncestor, 
                                    AncestorType={x:Type UserControl}
                                   }
                 }" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </ListBox>

标签: c#wpfmvvm

解决方案


一种方法可能是设置您的 ListBox x:Name,然后将 CommandParameter 绑定到其 SelectedIndex 属性,如下所示:

<ListBox x:Name="lb">
        <i:Interaction.Triggers>
            <i:EventTrigger
                EventName="MouseDoubleClick">
                <i:InvokeCommandAction
                    Command="{
                              Binding Path=SelectPath,  
                              Mode=OneTime, 
                              RelativeSource={
                                            RelativeSource Mode=FindAncestor, 
                                            AncestorType={x:Type UserControl}
                                     }
                 }" 
                    CommandParameter="{Binding ElementName=lb, Path=SelectedIndex}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </ListBox>

推荐阅读