【问题标题】:ListBox SelectedItem is always nullListBox SelectedItem 始终为空
【发布时间】:2016-07-07 08:30:43
【问题描述】:

当我单击 ListBox 中的项目时,它总是返回 null。当然,我的 ListBox 已经填满了 :-)
我的 XAML 代码:

<Window x:Class="WpfTimeClock.AdminWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfTimeClock"
    mc:Ignorable="d"
    ResizeMode="CanMinimize"
    Title="AdminWindow" Height="527" Width="750">
<Grid>
    <ListBox x:Name="listBoxUsers" HorizontalAlignment="Left" Height="250" VerticalAlignment="Top" Width="200" Margin="10,10,0,0"/>
</Grid>
</Window>

我的 C#-代码:

TimeClockRepository<User> repUser = new TimeClockRepository<User>();

public AdminWindow()
{
    InitializeComponent();
    Loaded += AdminWindow_Loaded;
    listBoxUsers.PreviewMouseLeftButtonDown += ListBoxUsers_PreviewMouseLeftButtonDown;
}

private void ListBoxUsers_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var item = sender as ListBoxItem;
    if (item != null && item.IsSelected)
    {
        // why is sender always null??
    }
}

private void AdminWindow_Loaded(object sender, RoutedEventArgs e)
{
    listBoxUsers.ItemsSource = repUser.Get().ToList();
    listBoxUsers.DisplayMemberPath = "UserName";
}

当我点击它时,我的 sender 项目始终为空。问题是什么?感谢您的帮助!

【问题讨论】:

  • sender 不为空。它是一个列表框。 item 为 null,因为您无法将 ListBox 转换为 ListBoxItem。
  • 扩展@ASh 的评论:as 关键字将在可以转换时进行转换,并在无法执行转换时返回 null
  • 如果您想将列表框项作为发件人,请使用listBoxUsers.SelectionChanged += ListBoxUsers_SelectionChanged;

标签: c# wpf xaml listbox


【解决方案1】:

您应该处理ListViewItem,而不是ListBox。只需在ListView.ItemContainerStyle 中为ListViewItem 创建处理程序:

XAML:

<ListView ItemsSource={Binding YourItems}>
    <ListView.View>
        <GridView>
            <!-- Declare a GridViewColumn for each property -->
        </GridView>
    </ListView.View>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

代码隐藏:

private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var item = sender as ListViewItem;
    if (item != null && item.IsSelected)
    {
        //Do your actions
    }
}

【讨论】:

    【解决方案2】:

    您应该改用 SelectionChanged 事件。

    this.listBoxUsers.SelectionChanged += ListBoxUsers_SelectionChanged;
    
    private void ListBoxUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // do your things with listBoxUsers.SelectedItem
        // If you want to click same item more than once, set SelectedIndex to -1 after use it.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多