具有相同事件的文本框的继承

时间:2014-01-26 14:32:42

标签: c# wpf xaml

我需要创建10个或更多具有相同事件和逻辑的文本框, 而不是复制和粘贴它有一个选项来创建它一次而不是从它继承? 例如,name1将从名称

继承
<TextBox x:Name="Name" AcceptsReturn="True" AllowDrop="True"
         PreviewDragEnter="DropText_PreviewDragEnter"
         PreviewDrop="DropText_PreviewDrop"
         SelectionChanged="listbox_SelectionChanged"
         HorizontalAlignment="Left" Height="25" TextWrapping="Wrap"
         VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0" 
         Grid.Row="2" />

<TextBox x:Name="name1" AcceptsReturn="True" AllowDrop="True"
         PreviewDragEnter="DropText_PreviewDragEnter"
         PreviewDrop="DropText_PreviewDrop"
         SelectionChanged="listbox_SelectionChanged"
         HorizontalAlignment="Left" Height="25" TextWrapping="Wrap"
         VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0"
         Grid.Row="2" />

3 个答案:

答案 0 :(得分:2)

您可以创建文本框的控件模板作为资源。将资源标记为x:Shared = False

<Grid.Resources>
   <ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False">
<Grid.Resources>

在文本框的其他实例上使用此模板。

    <TextBox x:Name="name1" Grid.Row="0" Template="{StaticResource TxtBoxTemplate}"/>
    <TextBox x:Name="name2" Grid.Row="1" Template="{StaticResource TxtBoxTemplate}"/>
    <TextBox x:Name="name3" Grid.Row="2" Template="{StaticResource TxtBoxTemplate}"/>

完整代码如下。

<Grid>
    <Grid.Resources>
            <ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False">
            <TextBox x:Name="Name"  
                AcceptsReturn="True"
                AllowDrop="True" 
                PreviewDragEnter="DropText_PreviewDragEnter"
                PreviewDrop="DropText_PreviewDrop"

                SelectionChanged="listbox_SelectionChanged"
                HorizontalAlignment="Left" Height="25" TextWrapping="Wrap" 
                VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0" Grid.Row="2"
                     TextChanged="Name_OnTextChanged"
              />
        </ControlTemplate>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBox x:Name="name1" Grid.Row="0" Template="{StaticResource TxtBoxTemplate}"/>
    <TextBox x:Name="name2" Grid.Row="1" Template="{StaticResource TxtBoxTemplate}"/>
    <TextBox x:Name="name3" Grid.Row="2" Template="{StaticResource TxtBoxTemplate}"/>
</Grid>

答案 1 :(得分:1)

正如HighCore在评论中提到的那样,ItemsControl可能对您有帮助。

创建 ItemsControl并将ItemTemplate设置为包含TextBox 。 (创建ObjectDataProvider以返回所需的textBoxes数量)

<Grid>
   <Grid.Resources>
       <ObjectDataProvider x:Key="EnumerableRange"
                     xmlns:sys="clr-namespace:System;assembly=mscorlib"
                     xmlns:linq="clr-namespace:System.Linq;assembly=System.Core"
                     ObjectType="{x:Type linq:Enumerable}" MethodName="Range">
                <ObjectDataProvider.MethodParameters>
                    <sys:Int32>1</sys:Int32>
                    <sys:Int32>15</sys:Int32>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
   </Grid.Resources>

  <ItemsControl ItemsSource="{Binding Source={StaticResource EnumerableRange}}">
      <ItemsControl.ItemTemplate>
         <DataTemplate>
             <TextBox x:Name="Name"  
                      AcceptsReturn="True"
                      AllowDrop="True" 
                      PreviewDragEnter="DropText_PreviewDragEnter"
                      PreviewDrop="DropText_PreviewDrop"
                      SelectionChanged="listbox_SelectionChanged"
                      HorizontalAlignment="Left" Height="25"
                      TextWrapping="Wrap" 
                      VerticalAlignment="Top" Width="150" Grid.Column="4" 
                      Margin="0,50,0,0" Grid.Row="2"/>
         </DataTemplate>
      </ItemsControl.ItemTemplate>
  </ItemsControl>
</Grid>

<强>更新

您可以使用 Style然后在那里声明公共属性和事件,并简单地让多个TextBox 实例引用该样式。

<Grid>
   <Grid.Resources>
      <Style TargetType="TextBox">
          <Setter Property="AcceptsReturn" Value="True"/>
          <Setter Property="AllowDrop" Value="True"/>
          <Setter Property="HorizontalAlignment" Value="Left"/>
          <Setter Property="Height" Value="25"/>
          <Setter Property="Width" Value="150"/>
          <Setter Property="TextWrapping" Value="Wrap"/>
          <Setter Property="VerticalAlignment" Value="Top"/>
          <Setter Property="Margin" Value="0,50,0,0"/>
          <EventSetter Event="PreviewDragEnter"
                       Handler="DropText_PreviewDragEnter"/>
          <EventSetter Event="PreviewDrop"
                       Handler="DropText_PreviewDrop"/>
          <EventSetter Event="SelectionChanged"
                       Handler="listbox_SelectionChanged"/>
       </Style>
   </Grid.Resources>

   <TextBox x:Name="Name"/>
   <TextBox x:Name="Name1"/>
    ......
   <TextBox x:Name="Name15"/>
</Grid>

注意我没有在Style上设置x:Key,因此默认情况下它将应用于Grid中的所有TextBox。如果您不想设置x:Key并将其用于所有TextBox。

答案 2 :(得分:0)

#first method:创建一个名为customTextbox.cs的类,并记下textbox的属性和事件

public class customTextbox:TextBox
{
   public customTextbox():base()
   {
       this.AcceptsReturn = true;
       this.AllowDrop = true;
       this.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
       //set your remaining propreties
       this.PreviewDragEnter +=customTextbox_PreviewDragEnter;
       this.PreviewDragLeave +=customTextbox_PreviewDragLeave;
       this.SelectionChanged += customTextbox_SelectionChanged;         
   }

   void customTextbox_SelectionChanged(object sender, System.Windows.RoutedEventArgs e)
   {
       //code here
   }
   private void customTextbox_PreviewDragLeave(object sender, System.Windows.DragEventArgs e)
   {
       //code here
   }

   private void customTextbox_PreviewDragEnter(object sender, System.Windows.DragEventArgs e)
   {
      //code here 
   }     
}

和xaml

<Window x:Class="WpfApplication3.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"
    xmlns:hp="clr-namespace:WpfApplication3">

<Grid Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <hp:customTextbox Height="100" Width="100" Grid.Row="0"></hp:customTextbox>
    <hp:customTextbox Grid.Row="1" ></hp:customTextbox>
    <hp:customTextbox Grid.Row="2" ></hp:customTextbox>
</Grid>

#second方法:创建一个像这样的usecontrol(http://prntscr.com/2mren4)并将usercontrol替换为下面的文本框

<TextBox x:Class="WpfApplication3.CustomTB"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<!--set all properties and event you required inside textbox--> 

并在.cs文件中使用文本框而不是像下面的用户控件

  public partial class CustomTB : TextBox
{
    public CustomTB()
    {
        InitializeComponent();
    }
}

并在另一个窗口中添加

<Window x:Class="WpfApplication3.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"
    xmlns:hp="clr-namespace:WpfApplication3"> 
<Grid Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>          
    </Grid.RowDefinitions>       
    <hp:CustomTB Grid.Row="0"/> 
</Grid>