This is a migrated thread and some comments may be shown as answers.

Datatemplates with GridViewCell

2 Answers 247 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Paul Gallen
Top achievements
Rank 1
Paul Gallen asked on 28 May 2009, 12:35 PM
Hi,

What I am trying to achieve is this: Depending what I am bound to, the cell in edit mode will display different types of controls i.e a checkbox, combobox, TextBox etc.. Looking at the ControlTemplate named 'EditTemplate', below, is incorrect because you cannot have a DataTemplate as a child of a border. In the Microsoft data grid world you have something called a CellEditingTemplate, where a DataTemplate is permissible. I see in your grid you have something named a CellEditTemplate, but, alas, I could not find any examples/documentation to view this object.

so....

1. Am I correct to use a style (named UDFCellViewStyle) with a trigger to determine if the grid is in edit mode
2. Can I use  the CellEditTemplate instead of a style trigger
3. How can I use my datatemplate for your cell in Edit mode
 <LocalFramework:UDFValueConverter  x:Key="UDFValueConverter" /> 
          
        <Style x:Key="ComboStyleUDFMultipleSelect" TargetType="{x:Type ComboBox}">  
            <Setter Property="ItemTemplate">  
                <Setter.Value> 
                    <DataTemplate> 
                        <StackPanel Orientation="Horizontal">  
                            <CheckBox  x:Name="chkSelected" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>  
                            <TextBlock Text="{Binding Path=Value}" TextWrapping="Wrap" x:Name="txtListValue" Margin="2,0,0,0"/>  
                        </StackPanel> 
                    </DataTemplate> 
                </Setter.Value> 
            </Setter> 
        </Style> 
          
        <ControlTemplate  x:Key="NormalTemplate" TargetType="telerikGridView:GridViewCell">  
            <Border BorderThickness="{TemplateBinding BorderThickness}"   
                                            BorderBrush="{TemplateBinding BorderBrush}"   
                                            Background="LightGreen" > 
                <TextBlock Text="{Binding Converter={StaticResource UDFValueConverter}}" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Text}" /> 
            </Border> 
        </ControlTemplate> 
          
        <ControlTemplate x:Key="EditTemplate" TargetType="telerikGridView:GridViewCell">  
            <Border BorderThickness="{TemplateBinding BorderThickness}"   
                                            BorderBrush="{TemplateBinding BorderBrush}"   
                                            Background="{TemplateBinding Background}">  
               <DataTemplate> 
                    <StackPanel x:Name="sp_Value" Orientation="Vertical" > 
                        <toolkit:DatePicker x:Name="dpValue" Visibility="Collapsed"   SelectedDateFormat="Short" /> 
                        <TextBox  x:Name="txtValue"  Visibility="Collapsed"  /> 
                        <ComboBox  x:Name="cboValue" Visibility="Collapsed" DisplayMemberPath="Value" SelectedItem="{Binding CityInfo, Mode=TwoWay}"  ItemsSource="{Binding UDFListValues, Source={StaticResource CMXLookUPsDS}}"></ComboBox> 
                        <CheckBox x:Name="chkValue" Visibility="Collapsed"></CheckBox> 
                </StackPanel> 
                    <DataTemplate.Triggers> 
                        <DataTrigger Binding="{Binding Path=UDF.UDFDataType.Name}" Value="System.String">  
                            <Setter TargetName="txtValue" Property="Visibility" Value="Visible"></Setter> 
                        </DataTrigger> 
                        <MultiDataTrigger> 
                            <MultiDataTrigger.Conditions> 
                                <Condition Binding="{Binding Path=UDF.UDFDataType.Name}" Value="System.Collections.ArrayList" /> 
                                <Condition Binding="{Binding Path=UDF.MultpleSelect}" Value="true" /> 
                            </MultiDataTrigger.Conditions> 
                            <Setter TargetName="cboValue" Property="Visibility" Value="Visible" /> 
                            <Setter TargetName="cboValue" Property="Style" Value="{StaticResource ComboStyleUDFMultipleSelect}"  /> 
                        </MultiDataTrigger> 
 
                        <MultiDataTrigger> 
                            <MultiDataTrigger.Conditions> 
                                <Condition Binding="{Binding Path=UDF.UDFDataType.Name}" Value="System.Collections.ArrayList" /> 
                                <Condition Binding="{Binding Path=UDF.MultpleSelect}" Value="false" /> 
                            </MultiDataTrigger.Conditions> 
                            <Setter TargetName="cboValue" Property="Visibility" Value="Visible"></Setter> 
                        </MultiDataTrigger> 
 
                        <DataTrigger Binding="{Binding Path=UDF.UDFDataType.Name}" Value="System.Boolean">  
                            <Setter TargetName="chkValue" Property="Visibility" Value="Visible"></Setter> 
                        </DataTrigger> 
                        <DataTrigger Binding="{Binding Path=UDF.UDFDataType.Name}" Value="System.DateTime">  
                            <Setter TargetName="dpValue" Property="Visibility" Value="Visible"></Setter> 
                        </DataTrigger> 
                    </DataTemplate.Triggers> 
                </DataTemplate> 
                      
                
            </Border> 
        </ControlTemplate> 

 

 

<Style x:Key="UDFCellViewStyle" TargetType="telerikGridView:GridViewCell">

 

 

 

<Style.Triggers>

 

 

 

<Trigger Value="False" Property="IsInEditMode">

 

 

 

<Setter Property="telerik:GridViewCell.Template" Value="{StaticResource NormalTemplate}"/>

 

 

 

</Trigger>

 

 

 

<Trigger Value="True" Property="IsInEditMode">

 

 

 

<Setter Property="telerik:GridViewCell.Template" Value="{StaticResource EditTemplate}"/>

 

 

 

</Trigger>

 

 

 

</Style.Triggers>

 

 

 

</Style>

 


Thanks

P

2 Answers, 1 is accepted

Sort by
0
Paul Gallen
Top achievements
Rank 1
answered on 28 May 2009, 03:30 PM
OK Ifound where the datatemplate triggers should go (taking the example in the last post.) They belong in the ControlTemplate triggers node, see below.



   <ControlTemplate x:Key="EditTemplate" TargetType="telerikGridView:GridViewCell"  > 
            <Border   BorderThickness="{TemplateBinding BorderThickness}"   
                                            BorderBrush="{TemplateBinding BorderBrush}"   
                                            Background="Green" > 
 
 
                <StackPanel x:Name="sp_Value" Orientation="Vertical" > 
                    <toolkit:DatePicker x:Name="dpValue" Visibility="Collapsed"   SelectedDateFormat="Short" /> 
                    <TextBox  x:Name="txtValue"  Visibility="Collapsed"  /> 
                    <ComboBox  x:Name="cboValue"  IsVisibleChanged="cboValue_IsVisibleChanged"  Visibility="Collapsed" DisplayMemberPath="Value" SelectedItem="{Binding CityInfo, Mode=TwoWay}"  ItemsSource="{Binding UDFListValues, Source={StaticResource CMXLookUPsDS}}"></ComboBox> 
                    <CheckBox x:Name="chkValue" Visibility="Collapsed"></CheckBox> 
                </StackPanel> 
 
            </Border> 
            <ControlTemplate.Triggers > 
                <DataTrigger Binding="{Binding Path=UDF.UDFDataType.Name}" Value="System.String">  
                    <Setter TargetName="txtValue" Property="Visibility" Value="Visible"></Setter> 
                </DataTrigger> 
                <MultiDataTrigger> 
                    <MultiDataTrigger.Conditions> 
                        <Condition Binding="{Binding Path=UDF.UDFDataType.Name}" Value="System.Collections.ArrayList" /> 
                        <Condition Binding="{Binding Path=UDF.MultpleSelect}" Value="true" /> 
                    </MultiDataTrigger.Conditions> 
                    <Setter TargetName="cboValue" Property="Visibility" Value="Visible" /> 
                    <Setter TargetName="cboValue" Property="Style" Value="{StaticResource ComboStyleUDFMultipleSelect}"  /> 
                    <Setter TargetName="cboValue" Property="ItemsSource" Value="{Binding Converter={StaticResource UDFValueListConverter}}" ></Setter>  
                </MultiDataTrigger> 
 
                <MultiDataTrigger> 
                    <MultiDataTrigger.Conditions> 
                        <Condition Binding="{Binding Path=UDF.UDFDataType.Name}" Value="System.Collections.ArrayList" /> 
                        <Condition Binding="{Binding Path=UDF.MultpleSelect}" Value="false" /> 
                    </MultiDataTrigger.Conditions> 
                    <Setter TargetName="cboValue" Property="Visibility" Value="Visible"></Setter> 
                    <Setter TargetName="cboValue" Property="ItemsSource" Value="{Binding Converter={StaticResource UDFValueListConverter}}" ></Setter>  
                </MultiDataTrigger> 
 
                <DataTrigger Binding="{Binding Path=UDF.UDFDataType.Name}" Value="System.Boolean">  
                    <Setter TargetName="chkValue" Property="Visibility" Value="Visible"></Setter> 
                </DataTrigger> 
                <DataTrigger Binding="{Binding Path=UDF.UDFDataType.Name}" Value="System.DateTime">  
                    <Setter TargetName="dpValue" Property="Visibility" Value="Visible"></Setter> 
                </DataTrigger> 
            </ControlTemplate.Triggers> 
                  
        </ControlTemplate> 

I would still like to know the following:-

1. Am I correct to use a style (named UDFCellViewStyle) with a trigger to determine if the grid is in edit mode
2. Can I use  the CellEditTemplate instead of a style trigger
3. How do I access the control cboValue in Code (in particular) <RadGridView>.BeginningEdit
0
Nedyalko Nikolov
Telerik team
answered on 30 May 2009, 02:01 PM
Hi Paul Gallen,

About #1 and #2: Generally you have to use CellStyle property if there are no other options. By overriding the whole style you have to care about a lot of things (such as Invalid and MouseOver visual states) which come out of the box if you use the default style. So I can suggest you to use CellEditTemplate property instead.

About #3: At the time when RadGridView.BeginningEdit is raised, the GridViewCell.Editor is not created so you cannot access an object within the EditTemplate. I saw in your xaml that you handle ComboBox.IsVisibleChanged event, so I think that this event or ComboBox.Loaded can help you to do anything with this control before actual start of the edit.

We will consider adding an event that occurs after GridViewCell.Editor is loaded for one of our further releases.

Kind regards,
Nedyalko Nikolov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Paul Gallen
Top achievements
Rank 1
Answers by
Paul Gallen
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or