ListBox SelectedIndex как параметр команды - PullRequest
1 голос
/ 08 мая 2020

Как параметр 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>

EDIT:

Что я сделал, и он работает: создано новое свойство 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>

1 Ответ

3 голосов
/ 08 мая 2020

Одним из способов может быть установка вашего 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...