I have a custom control which is rendered as a cell in a data grid (Infragistics). All cells are displayed with its read-only template. The cells can be selected (provided by Infragistics).
Now if the user press for example "3" I change the template from read-only to an edit template. Which works fine.
The edit template contains a text box that should have the focus afterwards and also receive the keypress, eg. 3. The problem is that at the time I receive the keypress and change the template, it is not rendered yet of course.
Control
public class Cell
{
//dependency properties
public DataTemplate ReadOnlyTemplate;
public DataTemplate EditTemplate;
public DataTemplate CurrentTemplate;
OnPreviewKeyDown()
{
CurrentTemplate = EditTemplate;
}
}
Xaml
<Style TargetType="{x:Type local:Cell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Cell}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentControl HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Content="{Binding DataContext, RelativeSource={RelativeSource Self}}"
ContentTemplate="{Binding CurrentTemplate, RelativeSource={RelativeSource TemplatedParent}}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I cannot use OnApplyTemplate
because the template is rendered directly within the cell but in one of the children.
Here are some ideas I came up with to solve
- collect the keypress events until the template is rendered and then focus the textbox in the edit template and push the keypress events to it
- "somehow" put the rendering in a priority dispatcher and wait "sometime" until it is rendered and then do the GUI actions
Can someone point me in the right direction?