views:

228

answers:

1

Hi,

Ok. This is a tricky one: (for me at least)

I have a datagrid. A column of the datagrid is a simple <DataGridTemplateColumn> with its CellTemplate containing a <DataTemplate> which contains a <ComboBox>

 <my:DataGrid Name="dataGridMain" AutoGenerateColumns="False">
        <my:DataGrid.Columns>
            <my:DataGridTemplateColumn Header="Food" >
                <my:DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <ComboBox Name="comboDataTemplate" Text="{Binding Path=Food, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                 ItemsSource="{Binding Source={StaticResource resFoodLookups}}"
                                 DisplayMemberPath="FoodName" SelectedValuePath="FoodID" IsEditable="True" />
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
            </my:DataGridTemplateColumn>
        </my:DataGrid.Columns>
    </my:DataGrid>

All is working fine. Each combobox is bound to a static list - I know this because I told it to do so in the ItemsSource="{Binding Source={StaticResource resFoodLookups}}" statement.

But my requirement is that this list will change from row-to-row. That is: each time a user types a new entry in the combobox list on one row, I want to have it available in the selection on the next row.

Basically, I want to create a new list for the user each time the user inserts a new word in the combobox on any of the rows. (The combobox is editable).

Now, I can wire up the "ItemsSource=..." at run-time, but I'm only able to do this once thus the <DataTemplate> propagates the 'same' list to 'all' the comboboxes on 'all' the rows.

My thoughts are that I need to change the ItemsSource=... property on an object-by-object basis on each combobox that is created in memory after the DataTemplae has created them - but I have no idea how to do this !?!

Can anyone help or at least point me in the right direction?

Thanks... **

+1  A: 

What you need to do is perform 2 way data binding to your the ItemsSource, this way when the ItemSource is updated in one of the combo boxes it will auto update your original collection and therefore your other combo boxes as well.

Edit

What I normally do is use the MVVM pattern. It is worth some research if you are not already using a particular pattern on your application.

Using it to solve your problem i would do the following:

  1. Create a ViewModel (Lets call it MyViewModel) which has a collection of values called 'MyComboBoxItems' (It is important that you use ObservableCollection for the databinding to work)

  2. When I create the Window/Control that contains your table, I also create an instance of MyViewModel and set its the Window.DataContext=myViewModelInstance

  3. For your combobox binding use ItemsSource="{Binding Path=MyComboBoxItems, Mode=TwoWay}

Leigh Shayler
Yes, OK. This seems like a solution, but how do i set the combobox's ItemsSource property? At run-time, I can set the ItemsSource property on other comboboxes in code with no problems. e.g. myComboBox1.ItemsSource = _myListOfStrings; myComboBox2.ItemsSource = App.DataLayer.GetUniqueFoods(); But I cant seem to 'get at' the combo box that resides in the datagrid's DataTemplate. Something like: ((DataGridTemplateColumn)dataGridMain.Columns[0]).CellTemplate.DataTemplate.comboDataTemplate.ItemsSource = <blah>; (I know this is wrong - but you get my meaning?)
If i set the ItemsSource property of the combobox at build-time (with XAML), how can i 'load' my initial object with data? Maybe I'm over-complicating things?
Hi Leigh, ah yes I see. If the 'MyComboBoxItems' observable collection gets sorted, or an item gets deleted, the combobox entries disappear. I think that the databinding gets screwed up here. Can you suggest a way around this at all?
Jack could you give me some more details of your current problem? Is the databinding working at all now?
Leigh Shayler
Thanks Leigh. Yes, I got it working with your help.