首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ListBox SelectedIndex作为命令参数

ListBox SelectedIndex作为命令参数
EN

Stack Overflow用户
提问于 2020-05-07 21:40:45
回答 1查看 195关注 0票数 1

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

代码语言:javascript
运行
复制
<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,然后将属性作为命令参数传递。但有什么更好的方法吗?

代码语言:javascript
运行
复制
        <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>
EN

Stack Overflow用户

回答已采纳

发布于 2020-05-07 22:20:08

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

代码语言:javascript
运行
复制
<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>
票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61668216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档