C# 按键时更改数据模板并将按键发送到模板
我有一个自定义控件,它在数据网格Infragistics中呈现为单元格。所有单元格都以其只读模板显示。这些细胞可以由Infragistics选择 现在,如果用户按示例3,我会将模板从只读更改为编辑模板。这很好用 编辑模板包含一个文本框,该文本框应具有随后的焦点,并接收按键,如3。问题是,当我收到按键并更改模板时,它当然还没有呈现 控制 Xaml 我无法使用OnApplyTemplate,因为模板直接在单元格中呈现,而在其中一个子元素中呈现 以下是我想解决的一些想法 收集按键事件,直到呈现模板,然后聚焦编辑模板中的文本框并将按键事件推送到其中 以某种方式将渲染放在优先级调度器中,等待渲染完成,然后执行GUI操作C# 按键时更改数据模板并将按键发送到模板,c#,wpf,datatemplate,C#,Wpf,Datatemplate,我有一个自定义控件,它在数据网格Infragistics中呈现为单元格。所有单元格都以其只读模板显示。这些细胞可以由Infragistics选择 现在,如果用户按示例3,我会将模板从只读更改为编辑模板。这很好用 编辑模板包含一个文本框,该文本框应具有随后的焦点,并接收按键,如3。问题是,当我收到按键并更改模板时,它当然还没有呈现 控制 Xaml 我无法使用OnApplyTemplate,因为模板直接在单元格中呈现,而在其中一个子元素中呈现 以下是我想解决的一些想法 收集按键事件,直到呈现模板,然
有人能给我指出正确的方向吗?我最后得出以下结论
cell.Dispatcher?.BeginInvoke(DispatcherPriority.Render, new Action(() => OnCellEnteredEditMode(cell)));
private static void OnCellEnteredEditMode(Cell cell)
{
//traverse through the visual tree of the cell down to the first item which I can set to focus to
//use win32 API to send key press
}
您是否尝试使用DataTemplateSelector?例如,为单元格设置ContentTemplateSelector属性并将其绑定到文本框文本
<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>
cell.Dispatcher?.BeginInvoke(DispatcherPriority.Render, new Action(() => OnCellEnteredEditMode(cell)));
private static void OnCellEnteredEditMode(Cell cell)
{
//traverse through the visual tree of the cell down to the first item which I can set to focus to
//use win32 API to send key press
}