Form with ComboBoxes bound to the same source.
-
Apologies if the question isn't clear, I'm struggling to find the right terminology. I have a entity that has properties which are the same entity i.e.:
public class Unit
{
public int Id {get;set;}
public string Name {get;set;}public int ParentUnitId {get;set;}
}
On my WPF form I'd like a combobox for the user to select which Unit is the ParentUnitId. So far I've gotten it by using a List, but this a) doesn't update if you add a new Unit, b) doesn't seem 'right', I feel like I should be able to achieve this with binding alone.
If I just have my XAML starting the ComboBoxes ItemSources are the same source as the Form, then they act as filters/selectors and the whole form moves to that record. I'm using a CollectionViewSource as the DataContext for my Window.
-
Apologies if the question isn't clear, I'm struggling to find the right terminology. I have a entity that has properties which are the same entity i.e.:
public class Unit
{
public int Id {get;set;}
public string Name {get;set;}public int ParentUnitId {get;set;}
}
On my WPF form I'd like a combobox for the user to select which Unit is the ParentUnitId. So far I've gotten it by using a List, but this a) doesn't update if you add a new Unit, b) doesn't seem 'right', I feel like I should be able to achieve this with binding alone.
If I just have my XAML starting the ComboBoxes ItemSources are the same source as the Form, then they act as filters/selectors and the whole form moves to that record. I'm using a CollectionViewSource as the DataContext for my Window.
Don't use a
List<Unit>
as the collection behind yourItemsSource
useObservableCollection<Unit>
as this raises change notifications when the number of items in the list changes. As far as the rest of your question goes, I'm really not clear what you're trying to achieve here. -
Don't use a
List<Unit>
as the collection behind yourItemsSource
useObservableCollection<Unit>
as this raises change notifications when the number of items in the list changes. As far as the rest of your question goes, I'm really not clear what you're trying to achieve here.Yea I understand about the List / ObservableCollection bit. I'll have another go:) On my form, which allows you enter new units and change their details, one of the details is 'which is the parent unit'. This is a combobox, which should allow the user to select from the list of units. Its the correct setup of this combobox that I'm having problems with. I *think* I should be able to achieve this with pure XAML, but I'm not sure how. As I think the pure XAML solution will also have the advantage that if a new unit is added on the form, the combobox lists will all contain the new unit (as they're all the same source in some sense), which my current solution of a List wont have. And unless I've missed something, just changing
List
toObservableCollection
wont resolve that will it? -
Yea I understand about the List / ObservableCollection bit. I'll have another go:) On my form, which allows you enter new units and change their details, one of the details is 'which is the parent unit'. This is a combobox, which should allow the user to select from the list of units. Its the correct setup of this combobox that I'm having problems with. I *think* I should be able to achieve this with pure XAML, but I'm not sure how. As I think the pure XAML solution will also have the advantage that if a new unit is added on the form, the combobox lists will all contain the new unit (as they're all the same source in some sense), which my current solution of a List wont have. And unless I've missed something, just changing
List
toObservableCollection
wont resolve that will it?It would help if you could show us your ViewModel. That way I should be able to get a clearer idea. As it is, I can see 3 or 4 things that you could be asking for and I'd hate to send you up a blind alley. Oh, and if you need to add new items, you definitely need the OC. A list will not notify the ComboBox that there are new items.
-
It would help if you could show us your ViewModel. That way I should be able to get a clearer idea. As it is, I can see 3 or 4 things that you could be asking for and I'd hate to send you up a blind alley. Oh, and if you need to add new items, you definitely need the OC. A list will not notify the ComboBox that there are new items.
Ah, well right now we're not using VM. There's not a massive amount of experience, so we want to start slow and with what we're used to, so thats CodeBehind. I've pasted, the XAML, Code Behind and entity here (given the size didn't seem sensible to flood the forum) Pastie[^] So each Unit has three fields (BusinessAreaId, DivisionalUnitId, ParentUnitId), that also reference a Unit, so I would like 3 comboboxes each with a list of units inside, and ideally as you add units, then the combobox items also grow. My current ef entity load code is:
BackgroundWorker worker = new BackgroundWorker(); //the action to run worker.DoWork += (s, ev) => { this.Dispatcher.Invoke(new Action(() => UIHelper.ProgressBarRun(true))); context.Units.Load(); context.UnitTypes.Load(); unitNames = new ObservableCollection( context.Units.Local.Select(f => new UnitName() { Id = f.Id, Name = f.Name })); }; //what to run when the above completes worker.RunWorkerCompleted += (s, ev) => { viewSource.Source = context.Units.Local; unitTypeIdComboBox.ItemsSource = context.UnitTypes.Local; businessAreaIdComboBox.ItemsSource = unitNames; divisionalUnitIdComboBox.ItemsSource = unitNames; parentUnitIdComboBox.ItemsSource = unitNames; this.Dispatcher.Invoke(new Action(() => { UIHelper.ProgressBarRun(false); navFirstButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); })); }; //start the background work worker.RunWorkerAsync();
-
Apologies if the question isn't clear, I'm struggling to find the right terminology. I have a entity that has properties which are the same entity i.e.:
public class Unit
{
public int Id {get;set;}
public string Name {get;set;}public int ParentUnitId {get;set;}
}
On my WPF form I'd like a combobox for the user to select which Unit is the ParentUnitId. So far I've gotten it by using a List, but this a) doesn't update if you add a new Unit, b) doesn't seem 'right', I feel like I should be able to achieve this with binding alone.
If I just have my XAML starting the ComboBoxes ItemSources are the same source as the Form, then they act as filters/selectors and the whole form moves to that record. I'm using a CollectionViewSource as the DataContext for my Window.
Resolved it, adding design time data helped...though weirdly with it working the design time data now looks wrong. I'm not 100% this is fully correct, or the best or simplest way of doing this...but it does seem to work so far. I've got a few
CollectionViewSource
s, but the first and last are the only two relevant to this.I needed the
CollectionViewSource
unitNameViewSource, so that I could set theItemsSource
on theComboBox
to something. If I didn't set it, or set it to {Binding} I got false values on the first record. TheComboBox
s are defined as:The other thing I think was important was to bind to BusinessArea and not the BusinessAreaId column. Let EF faff with translating that! My code behind is then:
private void Page\_Loaded(object sender, RoutedEventArgs e) {