【问题标题】:Different Commands For Different Events in a ListBox列表框中不同事件的不同命令
【发布时间】:2014-07-04 22:51:36
【问题描述】:

我有以下列表框:

<ListBox ItemsSource="{Binding CityList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock x:name="Name" Text="{Binding Name }" />
                <TextBlock x:name="Country" Text="{Binding  Country}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

下面的文章很好地解释了如何实现 SelectionChanged :

<phone:LongListSelector x:Name="Results" Margin="0,0,-12,0" ItemsSource="{Binding Events}" SelectedItem="{Binding Event}">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0,0,0,17">
                <TextBlock Text="{Binding benchmark.name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                <TextBlock Text="{Binding summary}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>
            </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
            <command:EventToCommand Command="{Binding WodSelectedCommand, Mode=OneWay}" CommandParameter="{Binding Path=SelectedItem, ElementName=Results}" PassEventArgsToCommand="False"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</phone:LongListSelector>

但在本例中,Selection Changed 对于我的示例中的每个文本块都是相同的,我希望我的两个文本块有两个不同的命令。

我该怎么做?

【问题讨论】:

    标签: c# .net mvvm listbox mvvm-light


    【解决方案1】:

    将事件触发器放在DataTemplate 中。这应该让你有两个不同的命令来点击NameCountry

    <ListBox ItemsSource="{Binding CityList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock x:name="Name" Text="{Binding Name }" >
                       <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Tap">
                                <command:EventToCommand Command="{Binding NameSelectedCommand, Mode=OneWay}" CommandParameter="{Binding Path=SelectedItem, ElementName=CityList}" PassEventArgsToCommand="False"/>
                            </i:EventTrigger>
                       </i:Interaction.Triggers>
                    </TextBlock>
                    <TextBlock x:name="Country" Text="{Binding  Country}" >
                       <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Tap">
                                <command:EventToCommand Command="{Binding CountrySelectedCommand, Mode=OneWay}" CommandParameter="{Binding Path=SelectedItem, ElementName=CityList}" PassEventArgsToCommand="False"/>
                            </i:EventTrigger>
                       </i:Interaction.Triggers>
                    </TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 2019-11-13
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多