代码之家  ›  专栏  ›  技术社区  ›  Scott Baker

对ListBoxItem Selection执行按钮命令

  •  2
  • Scott Baker  · 技术社区  · 10 年前

    我有一个(简化为SO)列表框:

    <ListBox x:Name="CurriculumList"
                ItemsSource="{Binding FilteredCurriculums}"
                SelectedIndex="0"
                SelectionMode="Single"
                IsSynchronizedWithCurrentItem="True">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Name="TheButton" 
                        HorizontalContentAlignment="Stretch"
                        Content="{Binding DisplayMember}"
                        CommandParameter="{Binding Id}"
                        Command="{Binding OpenCurriculumEditViewCommand}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    Button 在中 DataTemplate 实际上没有被点击,所以 OpenCurriculumEditViewCommand 永远不会被执行。

    有人知道我怎么做吗?

    2 回复  |  直到 10 年前
        1
  •  0
  •   appa yip yip    10 年前

    OpenCurriculumEditViewCommand 当 ListBox 选择更改时,可以执行以下操作:

    第一:

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    

    然后。。

    <ListBox x:Name="CurriculumList"
            ItemsSource="{Binding FilteredCurriculums}"
            SelectedIndex="0"
            SelectionMode="Single"
            IsSynchronizedWithCurrentItem="True">
    
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction CommandParameter="{Binding Path=SelectedItem.Id, RelativeSource={RelativeSource AncestorType=ListBox}}" Command="{Binding OpenCurriculumEditViewCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListBox>
    
        2
  •  0
  •   brunnerh    10 年前

    您不应该使用 Button 如果你想让它在选择时触发的话。