将焦点设置在 ControlTemplate 中的控件上(第 2 部分)

发布于 2024-10-05 13:15:20 字数 2239 浏览 5 评论 0原文

我对最常见的 WPF 要求之一感到困惑。我已阅读这个问题 但我的解决方案的实现不起作用。

这是外观控件的标记:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfTest">
  <Style TargetType="{x:Type local:CustomControl}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:CustomControl}">
          <Border>
            <TextBox x:Name="myTextBox" />
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsFocused"
                     Value="True">
              <Setter Property="FocusManager.FocusedElement"
                      Value="{Binding ElementName=myTextBox}" />
              <Setter TargetName="myTextBox"
                      Property="Background"
                      Value="Green" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

这是包含 CustomControl 实例的 Window 的标记:

<Window x:Class="WpfTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTest"
        Title="Window1" Height="300" Width="300">

  <local:CustomControl x:Name="CCtl" />
</Window>

这是隐藏代码:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        Loaded += (RoutedEventHandler)delegate { CCtl.Focus(); };
    }
}

加载 Window1 时,文本框变为绿色(表明触发器工作),但焦点仍保留在CCtl 而不是文本框。毫无疑问,这与显示以下数据错误的输出有关:

找不到引用“ElementName=myTextBox”的绑定源。 BindingExpression:(无路径);数据项=空;目标元素是 '自定义控件'(名称='CCtl');目标属性是“FocusedElement” (输入“IInputElement”)。

我不知道为什么会出现这个错误。任何指点都感激不尽,谢谢。

I'm stumped on what must surely be one of the most common WPF requirements. I've read this question but my implementation of the solution does not work.

Here's the markup for the lookless control:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfTest">
  <Style TargetType="{x:Type local:CustomControl}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:CustomControl}">
          <Border>
            <TextBox x:Name="myTextBox" />
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsFocused"
                     Value="True">
              <Setter Property="FocusManager.FocusedElement"
                      Value="{Binding ElementName=myTextBox}" />
              <Setter TargetName="myTextBox"
                      Property="Background"
                      Value="Green" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

Here's the markup for the Window that contains an instance of the CustomControl:

<Window x:Class="WpfTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTest"
        Title="Window1" Height="300" Width="300">

  <local:CustomControl x:Name="CCtl" />
</Window>

And here's the code-behind:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        Loaded += (RoutedEventHandler)delegate { CCtl.Focus(); };
    }
}

When Window1 is loaded, the text box turns green (indicating that the trigger works) but focus remains with CCtl and not the text box. Doubtless this has to do with the output displaying the following data error:

Cannot find source for binding with reference 'ElementName=myTextBox'.
BindingExpression:(no path); DataItem=null; target element is
'CustomControl' (Name='CCtl'); target property is 'FocusedElement'
(type 'IInputElement').

I've no idea why this error is appearing. Any pointers gratefully received, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的心爱 2024-10-12 13:15:20

尝试将其用于触发器:

<Trigger Property="IsFocused" Value="True">
    <Setter TargetName="myTextBox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=myTextBox}" />
</Trigger>

错误告诉您它无法找到 myTextBox,因为该名称不在应用 FocusedElement 属性的范围内。在本例中,它位于 CCtl 实例本身上,无法看到其自己的模板内部。通过在模板内的某些内容上设置属性,绑定可以定位指定的元素。

Try using this for your Trigger instead:

<Trigger Property="IsFocused" Value="True">
    <Setter TargetName="myTextBox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=myTextBox}" />
</Trigger>

The error is telling you that it can't locate myTextBox because the name isn't in scope where the FocusedElement property is being applied. In this case that's on the CCtl instance itself, which can't see inside its own template. By setting the property on something inside the template the Binding can locate the named element.

回心转意 2024-10-12 13:15:20

我可能是错的,但我认为你的问题在于你的财产触发器。

通过将 TextBox 设置为聚焦,您实际上会使模板化父级上的 Trigger 无效,因此触发器会展开并反转将焦点设置在 TextBox 上(因此取消聚焦)它)。

I may be wrong, but I think your problem is with your property trigger.

By setting your TextBox to being focussed, you in effect invalidate the Trigger on the Templated Parent, so the trigger unwinds and reverses setting the focus on the TextBox (therefore un-focussing it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文