anhoppe
anhoppe

Reputation: 4507

View / Edit dynamic parameters in a grid

I'm evaluating the possibilities to view and edit parameters in a C#/WPF/MVVM application. I'm currently looking into some custom controls from WPF Woolkit Extended, Telerik, DevExpress and Syncfusion. I see two options, a PropertyGrid or a DataGrid-like control.

The problem: My parameter content is in a collection and PropertyGrids don't seem to like those, they like to be bound to objects with properties. On the other hand my parameters have different value types and that is something the Data Grids do not favor, there the columns always have the same value editor (e.g. Check box, Date picker etc).

Does anybody know a reliable control that supports binding to a collection and individual value editors per row?

Other requirements are hierarchical data representation, validation and a search function.

Upvotes: 1

Views: 652

Answers (2)

anhoppe
anhoppe

Reputation: 4507

After a while of investigation I found out that all frameworks have the ability to apply individual cell editors in columns.

This is usually done in the column definition. A grid has a Columns collection. Each column can set a CellTemplateSelector which was the key to my initial problem.

Here is a little snippet for the Telerik grid view, but down to the CellTemplateSelector they all behave the same, as far as I can judge. All the vendor of 3rd party UI libs seems to have copied the behviour of the WPF DataGrid, of course.

<telerik:RadGridView x:Name="radGridView" 
                          AutoGenerateColumns="False">
<telerik:RadGridView.Columns>
   <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Parameter" />
   <telerik:GridViewDataColumn DataMemberBinding="{Binding Value}" Header="Value">
      <telerik:GridViewDataColumn.CellTemplateSelector>
         <telerik:ConditionalDataTemplateSelector>
            <telerik:DataTemplateRule Condition="PropertyId &lt; 1">
               <DataTemplate>
                  <TextBox Text="{Binding Value, StringFormat=c}"/>
               </DataTemplate>
            </telerik:DataTemplateRule>
            <telerik:DataTemplateRule Condition="PropertyId > 0">
               <DataTemplate>
                  <CheckBox IsChecked="{Binding Value}" />
               </DataTemplate>
            </telerik:DataTemplateRule>
         </telerik:ConditionalDataTemplateSelector>
      </telerik:GridViewDataColumn.CellTemplateSelector>

   </telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>

In this example the ItmesSource of the grid is bound to my view model that contains Name, Value and PropertyId properties. Based on the PropertyId a control is selected. The thing I like about the Telerik control is that I don't need code-behind since the ConditionalDataTemplateSelector is available in that framework. Better for MVVM to keep your view clean of code behind, I guess.

UPDATE:

Just received an answer from Syncfusion, they say:

We can load different cell editors in one column. For that we have to Handle dataGrid.Model.QueryCellInfo even and we have to change the CellType based on data. We can't avoid code behind. But this can be achieved using Behaviors.

Upvotes: 1

Aasmund Eldhuset
Aasmund Eldhuset

Reputation: 37960

DataGrid lets you use a DataGridTemplateColumn, where you can specify a template that will be applied to each cell in that column. This template could contain a UserControl that you have defined, which analyzes its DataContext and shows the appropriate control. (This will be quite a bit of work, though, so if there exist out-of-the-box solutions for this, you should use that instead.)

Upvotes: 1

Related Questions