【问题标题】:get the index of a button in a x:Array of a ListBox (wpf)获取列表框 (wpf) 的 x:Array 中按钮的索引
【发布时间】:2010-12-30 05:03:42
【问题描述】:

你好 有没有办法在 ListBox 的 x:Array 中获取按钮的索引。

我知道我可以在单击后获取按钮信息(通过 sender 参数),但我无法获取它的索引。

ListBox.CurrentItem 和 ListBox.SelectedItem 不起作用

运气不好 int t = this.lstButtons.SelectedIndex;

t=this.lstButtons.Items.CurrentPosition;

t=lstButtons.ItemContainerGenerator.IndexFromContainer((Button)sender);

            <x:Array Type="Button" >

                <Button Content="Button1"   VerticalAlignment="Top" Width="149" Click="Button_Click" />
                <Button Content="Button2"    VerticalAlignment="Top" Width="149" Click="Button_Click"/>
                <Button Content="Button3" BorderThickness="4"   VerticalAlignment="Top" Width="149" Click="Button_Click"/>
            </x:Array>
            </ListBox.ItemsSource>

【问题讨论】:

  • 感谢您的指针 var bouts = lstButtons.ItemsSource.Cast

标签: wpf arrays button listbox indexing


【解决方案1】:

好吧,我会重新考虑这个问题,因为这听起来像是一种非常脆弱和迂回的方式来根据项目采取有条件的行动。但你可能会做的是:

int index = Array.IndexOf((Button[])listBox.ItemsSource, clickedButton);

【讨论】:

  • 感谢您的指针 var bouts = lstButtons.ItemsSource.Cast
猜你喜欢
  • 2013-05-17
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-14
相关资源
最近更新 更多