在ListBoxItem选择上执行Button命令

时间:2016-07-15 16:26:32

标签: wpf listbox

我有这个(简化为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>

我可以使用键盘在listBoxItems中上下导航以更改选择,但它不会更改详细信息视图 - Button中的DataTemplate实际上并未获得点击,所以OpenCurriculumEditViewCommand永远不会被执行。

任何人都知道我该怎么做?

2 个答案:

答案 0 :(得分:0)

如果您希望在选择时触发,则不应使用string commandString =string.format("INSERT INTO DATABASENAME(USERNAME, UPDATE_DATE) VALUES ('{0}', '{1}')", userName, updateDate.ToString("yyyy-MM-dd.HH.mm.ss.ffffff")); OdbcConnection con = new OdbcConnection(conString); OdbcCommand command = new OdbcCommand(commandString, con); //execute the stuff con.Open(); command.ExecuteNonQuery(); con.Close();

答案 1 :(得分:0)

好吧,如果您想在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>

然后..

{{1}}