简体   繁体   English

装订的列表框在单击时不会选择项目

[英]ListBox filled with binding doesn't select item on click

I'm trying to use a ListBox to choose an entry and then display a picture belonging to this selected entry. 我正在尝试使用ListBox选择一个条目,然后显示属于此选定条目的图片。 But just at the beginning I got my first problem: filling the ListBox with binding is working, but if I click on one line in my running program, it doesn't select the line. 但是,刚开始的时候我遇到了第一个问题:用绑定填充ListBox是可行的,但是如果我在运行的程序中单击一行,则不会选择该行。 I can just see the highlighted hover effect, but not select a line. 我只能看到突出显示的悬停效果,但不能选择一行。 Any ideas what my mistake could be? 任何想法我的错误可能是什么?

This is my XAML: 这是我的XAML:

        <ListBox x:Name="entrySelection" ItemsSource="{Binding Path=entryItems}" HorizontalAlignment="Left" Height="335" Margin="428,349,0,0" VerticalAlignment="Top" Width="540" FontSize="24"/>

And in MainWindow.xaml.cs I'm filling the ListBox with entries: 在MainWindow.xaml.cs中,我用条目填充ListBox:

private void fillEntrySelectionListBox()
    {
        //Fill listBox with entries for active user
        DataContext = this;
        entryItems = new ObservableCollection<ComboBoxItem>();
        foreach (HistoryEntry h in activeUser.History)
        {
            var cbItem = new ComboBoxItem();
            cbItem.Content = h.toString();
            entryItems.Add(cbItem);
        }
        this.entrySelection.ItemsSource = entryItems;
        labelEntrySelection.Text = "Einträge für: " + activeUser.Id;

        //show image matching the selected entry
        if (activeUser.History != null)
        {
            int index = entrySelection.SelectedIndex;
            if (index != -1 && index < activeUser.History.Count)
            {
                this.entryImage.Source = activeUser.History[index].Image;
            }
        }
    }

So I can see my ListBox correctly filled, but not select anything - so I can't go on with loading the picture matching the selected entry. 因此,我可以看到我的ListBox正确填充,但是什么也没选择-因此,我无法继续加载与所选条目匹配的图片。 I'm still quite new to programming, so any help would be great :) 我对编程还是很陌生,所以任何帮助都会很棒:)


EDIT: If someone takes a look at this thread later: here's the - quite obvious -solution 编辑:如果以后有人看一下这个线程:这是-很明显的-solution

XAML now looks like this XAML现在看起来像这样

<ListBox x:Name="entrySelection" ItemsSource="{Binding Path=entryItems}" HorizontalAlignment="Left" Height="335" Margin="428,349,0,0" VerticalAlignment="Top" Width="540" FontFamily="Siemens sans" FontSize="24">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Text}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Code behind to fill it: 后面的代码可以填充它:

//Fill listbox with entries for selected user
DataContext = this;
entryItems = new ObservableCollection<DataItem>();
foreach (HistoryEntry h in selectedUser.History)
{
    var lbItem = new DataItem(h.toString());
    entryItems.Add(lbItem);
}
this.entrySelection.ItemsSource = entryItems;
labelEntrySelection.Text = "Einträge für: " + selectedUser.Id;

And new Class DataItem: 和新的Class DataItem:

class DataItem
{
    private String text;

    public DataItem(String s)
    {
        text = s;
    }

    public String Text
    {
        get 
        { 
            return text; 
        }
    }
}

You are filling it with ComboBoxItem, which is not relevant to the ListBox, and also wrong by definition. 您用ComboBoxItem填充了它,它与ListBox不相关,并且在定义上也是错误的。

You need to have the ObservableCollection filled with data items. 您需要使ObservableCollection充满数据项。

Meaning, make a class that contains the data you want to store, and the ListBox will generate a ListBoxItem automatically per data item. 意思是,创建一个包含要存储的数据的类,ListBox将为每个数据项自动生成一个ListBoxItem。

http://www.wpf-tutorial.com/list-controls/listbox-control/ http://www.wpf-tutorial.com/list-controls/listbox-control/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM