在WPF中的控件模板中的控件上设置焦点控件、模板、焦点、WPF

2023-09-04 00:46:42 作者:依然很明媚

在我工作的一个应用程序,我们有一堆的自定义控件在Generic.xaml定义自己的CONTROLTEMPLATES。

In an application I'm working on, we have a bunch of custom controls with their ControlTemplates defined in Generic.xaml.

例如,我们的自定义文本框将类似于此:

For instance, our custom textbox would look similar to this:

<Style TargetType="{x:Type controls:FieldTextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type controls:FieldTextBox}">
                <Border BorderThickness="0" Margin="5">
                    <StackPanel ToolTip="{Binding Path=Field.HintText, RelativeSource={RelativeSource TemplatedParent}}">
                        <TextBlock Text="{Binding Path=Field.FieldLabel, RelativeSource={RelativeSource TemplatedParent}}" 
                                   HorizontalAlignment="Left" 
                                   />
                        <TextBox Width="{Binding Path=Field.DisplayWidth, RelativeSource={RelativeSource TemplatedParent}}" 
                                 HorizontalAlignment="Left" 
                                 Text="{Binding Path=Field.Data.CurrentValue, RelativeSource={RelativeSource TemplatedParent}}" 
                                 IsEnabled="{Binding Path=Field.IsEnabled, RelativeSource={RelativeSource TemplatedParent}}"
                                 ContextMenu="{Binding Source={StaticResource FieldContextMenu}}" >
                            <TextBox.Background>
                                <SolidColorBrush Color="{Binding Path=Field.CurrentBackgroundColor, RelativeSource={RelativeSource TemplatedParent}}"/>
                            </TextBox.Background>
                        </TextBox>
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Focusable" Value="True" />
    <Setter Property="IsTabStop" Value="False" />
</Style>

在我们的应用中,我们需要能够以编程方式将焦点设置在控件模板中的特定控制。

In our application, we need to be able to programatically set the focus on a particular control within the ControlTemplate.

在我们的C#code,我们可以根据我们的数据在特定的FieldTextBox。一旦我们有了正确的FieldTextBox,我们需要能够将焦点设置上包含的控件模板中的实际文本框。

Within our C# code, we can get to the particular "FieldTextBox" based on our data. Once we have the correct FieldTextBox, we need to be able to set the focus on the actual TextBox contained within the ControlTemplate.

我拿出最好的解决方案是在每个控件模板的主要控件设置一个名称(在这种情况下,它的文本框),如FocusableControl。

The best solution I've come up with is to set a name on the primary control in each control template (in this case it's the TextBox), such as "FocusableControl."

我的code(C-背后包含在$ C $为FieldTextBox)来接盘重点控制将是:

My code (contained in the code-behind for the FieldTextBox) to then set focus on the control would be:

    Control control = (Control)this.Template.FindName("FocusableControl", this);
    if (control != null)
    {
        control.Focus();
    }

该解决方案的工作。然而,没有人知道其他的解决方案,这将是比这个更有效呢?

This solution works. However, does anyone else know of a solution that would be more efficient than this?

推荐答案

在你的控制模板,您可以添加一个触发器,设置StackPanel中的的的FocusManager 以文本框你想专注。你触发的属性设置为{TemplateBinding IsFocused}所以它使用时,包含控制的重点。

Within your control template you can add a Trigger that sets the FocusedElement of the StackPanel's FocusManager to the textbox you want focused. You set the Trigger's property to {TemplateBinding IsFocused} so it fires when the containing control is focused.