gpt4 book ai didi

c# - ListBox SelectedIndex 作为命令参数

转载 作者:行者123 更新时间:2023-12-03 10:22:08 24 4
gpt4 key购买 nike

Command 参数如何绑定(bind)到 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 绑定(bind),然后将属性作为命令参数传递。但是有没有更好的方法呢?
        <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>

最佳答案

一种方法可能是设置您的 ListBox x:Name,然后将 CommandParameter 绑定(bind)到其 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>

关于c# - ListBox SelectedIndex 作为命令参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61668216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com