views:

188

answers:

2

This is probably an odd question but here I go (do let me know if this is a bad database design or just a weird situation I'm in).

I have two tables in my database: ProductGroup and Parameters. One contains information about various product groups as per its name and the other one contains information about various parameters that can be applied to each group (and thus make each group different).

Now to relate the two tables, there's a third table set up - GroupParameters with two primary keys: parameterId and groupId and third column being the value for the parameter for this group.

Now I have a screen that displays information about each group and its parameter values. The users should be able to edit these values also. Thus my question is how do I bind this Value (stored in the third table) to a textbox. And in general how would I bind a control in this situation.

Thanks guys!

EDIT: There's an important detail that I have left out. I want to use DataSet and DataTable objects to store the in-memory data from the database and also bind them to the controls. The reason for this is unrelated to the problem but it has to do with me having to track changes and be able to undo them at users' will :(

A: 

I don't see any real bad design here.

You can build the UI for this as a list of product groups user has to pick from (ListBox bound to the ProductGroups collection), then you can have a user-control with a ListBox, property named CurrentGroupId and some control buttons (e.g. "OK"). Bind CurrentGroupId to the ProductGroups ListBox's selected value. When CurrentGroupId changes or Parameters collection changes, you should filter this inner ListBox items using LINQ or whatever - basically refill the ListBox with GroupParameter-s where CurrentGroupId matches selected ProductGroupId. ListBox items should be GroupParameter-s objects.

Now (finally :) ) the answer to your question: you can data-template list's items to have a textbox, bound to the Value property like this (here 'local' is your namespace):

<DataTemplate DataType={x:Type local:GroupParameter}>
  <StackPanel Orientation="Horizontal">
    <TextBox Text="{Binding Value, Mode=TwoWay}" />
    <Button Content="OK" Click=INSERT_YOUR_OK_HANDLER_HERE />
    <Button Content="Cancel" Click=INSERT_YOUR_CANCEL_HANDLER_HERE />
  </StackPanel>
</DataTemplate>

If you need to give user an ability to add new parameters you can make some Add functionality which would add a new entry in the Parameters collection with currently selected group id. Then user can edit just added item in your user-control.

Hope it helps, and if it's hard to understand what i've just said, i could write most of that in code, i guess. Don't hesitate to ask me to :)

arconaut
A: 

Actually the best thing that worked for me was to use MultiBinding. I was not aware of this - it sucks having to learn on your own through Google search queries (even when learning with a book would be slow).

Alexandra