【问题标题】:How to know in Listbox which ListItem button is clicked?如何在 Listbox 中知道单击了哪个 ListItem 按钮?
【发布时间】:2011-10-11 15:13:14
【问题描述】:

我有列表框:

  <ListBox x:Name="FriendsRequestList">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <StackPanel>
                            <TextBlock Text="{Binding FullName}" Foreground="#FF316DCB"/>
                            <TextBlock Text="{Binding RequestText}" />
                            <StackPanel Orientation="Horizontal">
                                <Button Name="Accept" Content="Accept" Click="Accept_Click"  Foreground="#FF28901F" Background="#FFB4D8BA"/>
                                <Button Name="Decline" Content="Decline" Click="Decline_Click"  Foreground="#FF28901F" Background="#FFB4D8BA"/>
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
  </Listbox>

我在代码中尝试这些:

  private void Accept_Click(object sender, RoutedEventArgs e)
    {
        Button clickedButton = sender as Button;
        StackPanel st1 = clickedButton.Parent as StackPanel;
        StackPanel st2 = st1.Parent as StackPanel;
        StackPanel st3 = st2.Parent as StackPanel;
        object parentControl = st3.Parent;
        object obj = FriendsRequestList.Items[3];
        int index1 = FriendsRequestList.Items.IndexOf(obj);
        int index2 = FriendsRequestList.SelectedIndex; 
        int SenderId = FriendRequests.ElementAt(index).SenderID;
        UserServices.FriendRequestAccept(this, SenderId);
        UserServices.GetRequests(this);
    }

index2 为 -1,parentControl 为空。为什么 ListItem.SelectedIndex 是 -1? 我怎么知道点击了哪个 ListItem 按钮?

【问题讨论】:

  • 您使用的是 Expression Blend 4 吗?

标签: silverlight windows-phone-7 listbox controltemplate selectedindex


【解决方案1】:

ListBox.SelectedIndex 属性可能是 -1,因为Button 正在拦截点击事件,并且它没有被传播到ListBox。无论如何,你不需要索引来做你想做的事情。

假设您将ItemsSource 设置如下:

FriendsRequestList.ItemsSource = FriendRequests;

现在,假设FriendRequests 是某种包含FriendRequest 对象的集合,每个对象都包含FullNameRequestText 等属性,请将点击处理程序修改为

private void Accept_Click(object sender, RoutedEventArgs e)
{
  FriendRequest req = ( sender as Button ).DataContext as FriendRequest;
  int senderID = req.SenderID;
  ...
}

【讨论】:

  • FriendsRequestList.SelectedItem = clickedButton.DataContext; int index = FriendsRequestList.SelectedIndex; int SenderId = FriendRequests.ElementAt(index).SenderID;
  • @AramGevorgyan 这也有效,但 IMO 这样做是错误的。我不明白为什么你不能只从按钮的 DataContext 中获取 SenderID 而不是设置 ListBox.SelectedItem 并获取 ListBox.SelectedIndex
  • 对不起,我不明白你的意思。我完全设置了 ListBox.SelectedItem 然后得到它 SelectedIndex。
  • @AramGevorgyan 是的,但您根本不需要弄乱 ListBox。看我贴的代码。 ( sender as Button ).DataContext 您使用 FriendRequests.ElementAt(index) 获取的对象相同。因此,完全忘记 ListBox,只需转换发送者的 DataContext 并从中获取 SenderID
猜你喜欢
  • 2017-06-04
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
相关资源
最近更新 更多