如何在ControlTemplate内声明事件处理程序?

10

我有以下的ControlTemplate:

<ControlTemplate>
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Left" Width="400">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="18" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="45" />
        </Grid.ColumnDefinitions>
        <TextBox Grid.Column="1" Template="{StaticResource watermark}" HorizontalAlignment="Stretch" Margin="4,0,0,4" Tag="Number" />
        <TextBox Grid.Column="2" Template="{StaticResource watermark}" HorizontalAlignment="Stretch" Margin="4,0,0,4" Tag="Login" />
        <TextBox Grid.Column="3" Template="{StaticResource watermark}" HorizontalAlignment="Stretch" Margin="4,0,0,4" Tag="Password" />
        <Button Grid.Column="4" HorizontalAlignment="Stretch" Content="Add" Margin="4,0,0,4" Click="AddUser_Click"/>
    </Grid>
</ControlTemplate>

我应该如何编写 AddUser_Click 以访问文本框的 Text 属性?

更新: 为了明确问题,我知道如何在这里连接 Click 事件处理程序。问题是如何在其中读取文本框的内容,因为我不能给它们命名,因为它们在模板中。

4个回答

23

你可以给Button起一个名字叫做 "PART_Button"。然后在控件中重写OnApplyTemplate方法。在代码中,你可以这样写:

var btn = this.Template.FindName("PART_Button", this) as Button;
btn.Click += ...

6
事件处理程序在当前文件的 x:Class 指向的类中搜索,这使得您可以内联添加事件处理程序,而无需重写类并覆盖OnApplyTemplate,从而避免了添加处理程序的麻烦,无论您声明 ControlTemplate 的位置在哪里。

MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
  <Button Content="asdf">
    <Button.Style>
      <Style TargetType="Button">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="Button">
              <Button Content="{TemplateBinding Content}" Click="Button_Click"/>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Button.Style>
  </Button>
</Window>

MainWindow.xaml.cs:

using System.Windows;
namespace WpfApplication1
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show("Button clicked!");
    }
  }
}

2
如果您可以访问模板的父级(SelectedItem,FindVisualParent等),并且将名称应用于文本框,则可以执行此操作。例如,如果ControlTemplate是ComboBoxItem的,则可以这样做。
private void AddUser_Click(object sender, RoutedEventArgs e)
{
    ComboBoxItem comboBoxItem = GetVisualParent<ComboBoxItem>(button);
    TextBox textBox = comboBoxItem.Template.FindName("numberTextBox", comboBoxItem) as TextBox;
    //...
}
另一种获取控件模板中的文本框的方法是使用可视化树。可以像这样进行操作:
private void AddUser_Click(object sender, RoutedEventArgs e)
{
    Button button = sender as Button;
    Grid parentGrid = GetVisualParent<Grid>(button);
    List<TextBox> textBoxes = GetVisualChildCollection<TextBox>(parentGrid);
    foreach (TextBox textBox in textBoxes)
    {
        if (textBox.Tag == "Number")
        {
            // Do something..
        }
        else if (textBox.Tag == "Login")
        {
            // Do something..
        }
        else if (textBox.Tag == "Password")
        {
            // Do something..
        }
    }
}

实现GetVisualParent和GetVisualChildCollection方法

public static T GetVisualParent<T>(object childObject) where T : Visual
{
    DependencyObject child = childObject as DependencyObject;
    // iteratively traverse the visual tree
    while ((child != null) && !(child is T))
    {
        child = VisualTreeHelper.GetParent(child);
    }
    return child as T;
}

public static List<T> GetVisualChildCollection<T>(object parent) where T : Visual
{
    List<T> visualCollection = new List<T>();
    GetVisualChildCollection(parent as DependencyObject, visualCollection);
    return visualCollection;
}
private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : Visual
{
    int count = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < count; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        if (child is T)
        {
            visualCollection.Add(child as T);
        }
        else if (child != null)
        {
            GetVisualChildCollection(child, visualCollection);
        }
    }
}

1
我很抱歉,您的请求无法被翻译。请提供一种支持中文的编程语言或将内容翻译为英文。
 this.Template.FindName("control name", this)

方法 但是

 GetTemplateChild("control name")

工作正常。

'ResourceDictionary'存根

<Style x:Key="OGrid" TargetType="{x:Type local:OrientationGrid}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:OrientationGrid}" x:Name="PART_Control">
                    <CheckBox  x:Name="PART_CheckBox"/>
                            ...

自定义控件,OrientationGrid(方向网格),存根:
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        CheckBox chkbx = GetTemplateChild("PART_CheckBox") as CheckBox;
        chkbx.Checked += Chkbx_Checked;
    }

    private void Chkbx_Checked(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Event Raised");
    }
    ...

基于以下答案: WPF如何在代码中获取模板中的元素


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接