簡體   English   中英

WPF列表框selectedindex不起作用<no change>

[英]WPF Listbox selectedindex not working <no change>

我是WPF的新手。 我已經創建了一個列表框

<ListBox 
  Name="listBoxQuestion" 
  Grid.Row="1" Margin="3" 
  SelectionMode="Single" 
  MouseDown="listBoxQuestion_MouseDown" 
  SelectionChanged="listBoxQuestion_SelectionChanged" 
  DisplayMemberPath="Text">
</ListBox>

並且我使用listBoxQuestion.SelectedIndex獲取所選項目的索引。 但是它只能工作一次 ,這是我第一次單擊項目。 當我單擊其他項目時 ,索引不會更改。 它仍然保持當前值 我找不到我的代碼出了什么問題。 請幫我!

更新資料

有我的物品來源

public class ListTopicBinding : ObservableCollection<Question>
{
}

ListQuestionBinding listQuestionShowing = new ListQuestionBinding();

listBoxQuestion.ItemsSource = listQuestionShowing;

當我創建新項目時,我使用代碼

Question q = new Question();
q.Text = "*";
listQuestionShowing.Add(q);

我發現當我在Question類中刪除函數時

public override int GetHashCode()
    {
        return this.Text.GetHashCode();
    }

我的ListBox正常工作。 我不知道“ GetHashCode ”功能如何影響WPF中的 ListBox 我在WinForm中使用了此代碼,但沒有任何反應。 對不起我的英語不好

這是你想要的嗎?

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        listBoxQuestion.ItemsSource = new List<String> { "AAA", "BBB", "CCC" };
    }

    private void listBoxQuestion_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MessageBox.Show("You selected " + listBoxQuestion.SelectedItem.ToString());
    }

名稱與問題不符

ItemsSource="{Binding Path=listTopicBinding}"
DisplayMemberPath="Text"

private ListTopicClass listTopicBinding = new ListTopicClass();
public ListTopicBinding 
{
   get { return listTopicBinding ; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM