silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了silverlight / windows phone 7 selectedIndex列表框内按钮的问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我有一个包含简单项目列表的列表框.在我的xaml页面上,我有以下内容 <ListBox Name="listBox1"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Te
我有一个包含简单项目列表的列表框.在我的xaml页面上,我有以下内容

<ListBox Name="listBox1">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                    <TextBlock Text="{Binding firstNamE}"/>
                                    <TextBlock Text="{Binding lastNamE}"/>
                                    <Button BorderThickness="0" Click="buttonPerson_Click">
                                        <Image source="delete-icon.png"/>
                                    </Button>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

在我的代码隐藏中,我尝试抓取SELEctedIndex,这样我就可以从绑定到列表框的集合中删除该项.

private void buttonPerson_Click(object sender,RoutedEventArgs E)
        {

            // If SELEcted index is -1 (no SELEction) do nothing
            if (listBox1.SELEctedIndex == -1)
                return;

            myPersonList.removeAt(listBox1.SELEctedIndeX);

        }

但是,无论我在哪一行单击删除按钮,SELEctedIndex始终为-1

我错过了什么?

提前致谢!

解决方法

您可以通过将按钮的Tag属性设置为对象来执行您想要的操作,如下所示:

<Button BorderThickness="0" Click="buttonPerson_Click" Tag="{Binding BindsDirectlyTosource=TruE}">
     <Image source="delete-icon.png"/>
</Button>

然后在事件处理程序中,您可以这样做:

private void buttonPerson_Click(object sender,RoutedEventArgs E)
{
    myPersonList.remove((sender as Button).Tag);
}

不确定你的Person对象被调用了什么,所以我没有将Tag转换为它,但你可能不得不这样做,但看起来你对此感到满意.

XAML中是否缺少StackPanel启动元素?这可能只是一个疏忽,但如果这是您的实际代码,可能会导致一些问题.

大佬总结

以上是大佬教程为你收集整理的silverlight / windows phone 7 selectedIndex列表框内按钮的问题全部内容,希望文章能够帮你解决silverlight / windows phone 7 selectedIndex列表框内按钮的问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。