1

So we have a WPF DataGrid with some ComboBoxes in some of the cells where there is a foreign relation, but this means we have hundreds of ComboBoxes loaded at a time which takes too long. What we would like to do is load a label until it is clicked on at which point a ComboBox is loaded, we can easily do this with a TextBox.

The problem is, our comboboxes work and allow the user to change the foreign key value in the column by selecting from a number of display values (e.g. {Car, Dog, Cat}). But before the user clicks on the label, the label displays the foreign key value itself (e.g. {1, 2, 3}).

Any ideas on what we could do? Any help most appreciated!

<DataGridTemplateColumn Header="Column Name" SortMemberPath="Column Name"> 
    <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate>
            <Label Content="{Binding Path=DataViewBehindColumnName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate> 
    <DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 
           <ComboBox 
            DataContext="{DynamicResource ResourceKey=ViewModel}" 
            ItemsSource="{Binding Path=ReferenceTableName, 
                         Converter={StaticResource dataViewToListConverter}}" 
            DisplayMemberPath="ReferenceTableDisplayNamesColumn" 
            SelectedValuePath="ReferenceTablePrimaryKeyColumn" 
            SelectedValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, 
                           Path=Item[DataViewBehindColumnName]}"
            />
        </DataTemplate> 
     </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

Kind regards, Fugu

Fugu
  • 361
  • 2
  • 6
  • 17

2 Answers2

1

Create a ViewModel per line and there in provide a Property that represents the resolved string value and another property that represents the key. Bind the labels Content-property to the string-property and the combobox SelectedValue to the key-property.

Not MVVM speaking: Create a line item-wrapper that holds all properties that are necessary for the datagrid including one that holds the resolved string-value of your problematic property.

HCL
  • 36,053
  • 27
  • 163
  • 213
  • Is there a way to do this so we don't have to implement for every single foreign relation? Let us assume 15 foreign keys per table... we want to allow editing of 40 tables in our application - maintenance nightmare? – Fugu Aug 09 '11 at 10:17
  • @Fugu: I have not enough information to answer this question. I think some OR-Wrapper are capable of doing such things for you. In my case, I use a selfwritten tool to create such viewmodels. Maybe you will get an answer to this, if you post a question with the context of your OR-Mapper. If you don't use one but access database or another source directly, think about a helper class that does the resolution at an abstract level. – HCL Aug 09 '11 at 10:24
  • Many thanks. We're not doing object-relational; due to some data submission requirements we have DataSets behind and DataViews to bind controls to. Commands to change DataView filters. Converters for controls that need lists instead of DataViews. – Fugu Aug 09 '11 at 10:34
  • I dont know DataSets, but as far as I remember, they support navigation. Therefore, probably it should not be a big deal to get the resolved values. For the VievModel, if you have so many tables and all are totaly different, Maybe it would be a good idea to think about something index-based (bindings are possible to define on indexers). It's also a question about how stable your tables are (if they are changed frequently or if it will be static for many years)... – HCL Aug 09 '11 at 10:45
0

There's two things coming to my mind when I read your post

1) DataGrid Virtualization. This could save you some load.

2a)Build Proxies for your DataObjects with a bool property "ShowData". This is false by default and set to true when the control is clicked. You could do that with a commandBinding. If you're not using ORM, you can extend your DataSet SourceCode and add this functionality. This could end up being a sh*tload of work, though.

2b)ComboBoxes have an editable Template and a Non-Editable Template(You can find them easily in Blend).Bind the non-Editable Template's content to display a static String and the editable one to the relation. This will show your relation only when you try to edit the selectedItem. Again, if you use ORM like NHibernate, you can profit from LazyLoading features.

Sebastian Edelmeier
  • 4,095
  • 3
  • 39
  • 60