|
-
Jun 21st, 2011, 12:14 PM
#1
[RESOLVED] [WPF] ComboBox refusing to select an item
Hi,
I am creating a simple application for my own use that keeps track of scientific papers I have. Basically it just displays a list of papers, where each paper has properties like the title, the authors, any categories, the journal, the volume, pages, and the pdf file path.
I have a window EditPaperWindow that displays these properties for one paper and allows me to edit them (or create a new paper). The issue is the Journal. There is a ComboBox bound to an ObservableCollection<Journal> (which I fill in the constructor of the window). When the window loads, it should simply display the selected journal for that paper.
Saving a paper works just fine, I can select a journal from the combobox and it will be saved to the database. When I re-load that paper immediately after however (or any other time for that matter) the selected journal does not appear in the ComboBox.
There is one catch that may or may not be related to the problem, which is that the ComboBox is editable (IsEditable = True), so that if I want to create a new journal I can simply type the name of the journal and before saving the paper it will then save that journal to the database. This way I don't have to go to a separate 'journal editing' window to add a new journal before I create a new paper.
I have tried everything I could think of to get the combobox to display the journal, nothing works.
The first thing I tried is the way I'm used to in WPF, simply using databinding. I have already found that the SelectedItem property should go before the ItemsSource property in the XAML, but I already had that:
xml Code:
<!-- Journal--> <TextBlock Grid.Column="0" Grid.Row="3" Text="Journal:" /> <ComboBox Grid.Column="1" Grid.Row="3" IsEditable="True" SelectedItem="{Binding Path=Paper.Journal, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=Journals}" x:Name="journalsCombo" DisplayMemberPath="Name" />
In the code-behind for the window, I have this:
csharp Code:
public partial class EditPaperWindow : Window, INotifyPropertyChanged { public EditPaperWindow(Paper paper) { InitializeComponent(); this.DataContext = this; this.LoadLists(); _Paper = paper ?? new Paper(); } private readonly Paper _Paper; public Paper Paper { get { return _Paper; } } private EntityCollection<Journal> _Journals; public EntityCollection<Journal> Journals { get { return _Journals; } } private void LoadLists() { _Authors = AuthorManager.Instance.Load(); _Journals = JournalManager.Instance.Load(); _Categories = CategoryManager.Instance.Load(); } }
JournalManager is a autogenerated class and its Load method loads all Journals from the database.
Journal is an object with properties Id and Name.
EntityCollection<T> inherits ObservableCollection<T>.
From simple debugging I've deduced that the journals are loaded from the database correctly and the Paper.Journal property is the correct journal object. The ComboBox simply refuses to display it and stays blank (when I use the dropdown, it displays all journals correctly).
The next thing I tried is to get rid of the bound SelectedItem and just do it in code, so I've modified the constructor to:
csharp Code:
public EditPaperWindow(Paper paper) { InitializeComponent(); this.DataContext = this; this.LoadLists(); _Paper = paper ?? new Paper(); journalsCombo.SelectedItem = this.Paper.Journal; }
Nothing. From debugging again I can see that Paper.Journal is correct. Immediately after assigning the SelectedItem though, journalsCombo.SelectedItem is still null! It seems like it completely ignores the assignment...
I've tried various other approaches, such as using SelectedValue, setting the SelectedIndex, but nothing works, the ComboBox remains empty.
I'm at a loss here; shouldn't both of these approaches work just fine? Why is the ComboBox ignoring the SelectedItem assignment?
Thanks for any help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|