silverlightwindows phone 7 selectedIndex列表框内按钮的问题

silverlightwindows phone 7 selectedIndex列表框内按钮的问题,第1张

概述我有一个包含简单项目列表的列表框.在我的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列表框内按钮的问题所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1000948.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存