MouseRightClick 在 ListBox 上崩溃应用程序

MouseRightClick on ListBox crashes the app

当我用鼠标右键单击我的空列表框时,整个应用程序就崩溃了,我什至不知道如何调试它,把它放在哪里 try-catch 等等。

有人遇到过这样的问题吗?关于如何解决该问题的任何想法?

这是我在列表框中的 XAML:

    <ListBox x:Name="LstStat" HorizontalAlignment="Left" Height="129" Margin="10,10,0,0" 
             VerticalAlignment="Top" Width="330" FontSize="16"
             ItemsSource="{Binding StatisticsQueries}" Cursor="Arrow">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding Path=Name}" FontWeight="Medium" FontSize="18" FontFamily="HelveticaNeueCyr"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

也许 XAML 有什么问题?

更新

这是来自 .cs 的绑定

public ObservableCollection<Query> StatisticsQueries { get; private set; }
private void FillStatisticsList()
{
    const string statsQuery = "SELECT * FROM cached_queries WHERE is_statistics IS TRUE;";
    var connection = new MySqlConnection(DatabaseModel.ConnectionString);
    connection.Open();

    var cmd = new MySqlCommand(statsQuery, connection);

    cmd.ExecuteNonQuery();
    var reader = cmd.ExecuteReader();
    cmd.CommandType = CommandType.Text;

    StatisticsQueries = new ObservableCollection<Query>();

    while (reader.Read())
    {
        StatisticsQueries.Add(new Query
        {
            Id = reader["id"].ToString(),
            Autoschool = reader["autoschool"].ToString(),
            IsStatistics = reader["is_statistics"].ToString(),
            Name = reader["query_name"].ToString(),
            Text = reader["query_text"].ToString()
        });
    }
    connection.Close();
}

这是我在 Window_Loaded 活动中所做的:

await Task.Run(() => FillStatisticsList());

这就是与我的 ListBox 相关的所有代码,但它与 RightMouseClick 无关...为什么它不会在任何其他点击或其他情况下崩溃?

谢谢大家! 通过禁用鼠标右键单击我的列表框来解决它。

private void LstStat_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
}

private void LstStat_PreviewMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
}

尝试执行以下操作:包含 try-catch 块并显示错误消息:

private void FillStatisticsList()
{
    try
    {
        const string statsQuery = "SELECT * FROM cached_queries WHERE is_statistics IS TRUE;";
        var connection = new MySqlConnection(DatabaseModel.ConnectionString);
        connection.Open();

        var cmd = new MySqlCommand(statsQuery, connection);

        cmd.ExecuteNonQuery();
        var reader = cmd.ExecuteReader();
        cmd.CommandType = CommandType.Text;

        StatisticsQueries = new ObservableCollection<Query>();

        while (reader.Read())
        {
            StatisticsQueries.Add(new Query
            {
                Id = reader["id"].ToString(),
                Autoschool = reader["autoschool"].ToString(),
                IsStatistics = reader["is_statistics"].ToString(),
                Name = reader["query_name"].ToString(),
                Text = reader["query_text"].ToString()
            });
        }
        connection.Close();
    }
    catch (Exception ex) { MessageBox.Show(ex.Message); }
}

希望这可能有所帮助。