从风格中的风格获得控制

Get control from style within style

我有风格:

<Style TargetType="TextBox" x:Key="TextBoxStyle">
   <Setter Property="Template">
      <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Grid>
                     <Button x:Name="Button1" />
                </Grid>
            <ControlTemplate/>
      </Setter.Value>
   </Setter> 
</Style>

<Style TargetType="local:MyControl">
   <Setter Property="Template">
      <Setter.Value>
            <ControlTemplate TargetType="local:MyControl">
                <Grid>
                     ...
                     <TextBox x:Name="TextBox1" Style="{StaticRessource TextBoxStyle}/>
                     ...
                </Grid>
            <ControlTemplate/>
      </Setter.Value>
   </Setter> 
</Style>

问题是我无法获取 "Button1",隐藏代码:

public class MyControl : ItemsSource
{

    private TextBox _textBox1;
    private Button _button1;

    protected override void OnApplyTemplate()
    {
        _textBox1 = (TextBox)GetTemplateChild("TextBox1");

        base.OnApplyTemplate();

    }

}

在 wpf 中我可以做到:

_button1 = (Button)_textBox1.Template.FindName("Button1");

但是 uwp 中不存在 "FindName()" 方法,那么我怎样才能获得 Button1?

谢谢!

编辑:

如果我们查看 AutoSuggestBox's style,我们有样式 AutoSuggestBox 和 AutoSuggestBoxTextBoxStyle。 autosuggestbox 引发事件 "QuerySubmitted",该事件来自 AutoSuggestBoxTextBoxStyle 中的按钮 "QueryButton"。所以他可以访问按钮吗?

根据您的代码,您创建了一个模板化控件。 GetTemplateChild 方法用于从模板中获取子元素。上面的代码快照在 MyControl class 中,我们可以像找到 TextBox1 一样从 MyControl 模板中获取子元素。但是对于Button1,它不在MyControl的模板中(它在_textbox1的模板中),我们似乎无法获取它。

so how can i get the Button1 ?

我们可以在VisualTreeHelper渲染MyControl之后得到Button1。这是一个关于在MainPage中创建一个MyControl然后通过VisualTreeHelper获取Button1元素的演示。

Xaml代码

<local:MyControl x:Name="mycontrol" Height="200" Width="200"></local:MyControl>   
<Button x:Name="getbutton1" Click='getbutton1_Click' Content="Get button1"></Button>

代码隐藏

  private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  {
      if (depObj != null)
      {
          for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
          {
              DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
              if (child != null && child is T)
              {
                  yield return (T)child;
              }

              foreach (T childOfChild in FindVisualChildren<T>(child))
              {
                  yield return childOfChild;
              }
          }
      }
  }

  private void getbutton1_Click(object sender, RoutedEventArgs e)
  {
      Button _button1 = null;
      IEnumerable<Button> buttons = FindVisualChildren<Button>(mycontrol);
      foreach (var _button in buttons)
      {
          if (_button.Name == "Button1")
          {
             _button1 = _button;
          }
      }
      System.Diagnostics.Debug.WriteLine(_button1.Content);
  }

So he can have access to the button right ?

在那种情况下,请检查样式中的 QueryButton,它在 Grid 中,而不是 TextBox 中。您在 AutoSuggestBox 中看到的 'textbox' 实际上是一个 ContentControl,它与 QueryButton 在同一个 Grid 中。这样按钮就可以搞定了。