C# 具有相同事件的文本框的继承
我需要创建10个或更多具有相同事件和逻辑的文本框, 不是复制粘贴,而是选择创建一次,然后从中继承? 例如,name1将从name继承C# 具有相同事件的文本框的继承,c#,wpf,xaml,C#,Wpf,Xaml,我需要创建10个或更多具有相同事件和逻辑的文本框, 不是复制粘贴,而是选择创建一次,然后从中继承? 例如,name1将从name继承 <TextBox x:Name="Name" AcceptsReturn="True" AllowDrop="True" PreviewDragEnter="DropText_PreviewDragEnter" PreviewDrop="DropText_PreviewDrop" SelectionCha
<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" />
正如HighCore在评论中提到的,
ItemsControl
可能对您有所帮助
创建一个ItemsControl,并将itemstemplate设置为包含文本框。(创建ObjectDataProvider以返回所需的文本框数量)
1.
15
更新 然后,您可以使用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>
......
注意,我没有在样式上设置
x:Key
,因此默认情况下它将应用于网格中的所有文本框。如果您不希望设置x:Key
并将其用于所有文本框。正如HighCore在评论中提到的那样,ItemsControl
可能会对您有所帮助
创建一个ItemsControl,并将itemstemplate设置为包含文本框。(创建ObjectDataProvider以返回所需的文本框数量)
1.
15
更新 然后,您可以使用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>
......
注意,我没有在样式上设置
x:Key
,因此默认情况下它将应用于网格中的所有文本框。如果您不希望设置x:Key
并将其用于所有文本框。正如HighCore在评论中提到的那样,ItemsControl
可能会对您有所帮助
创建一个ItemsControl,并将itemstemplate设置为包含文本框。(创建ObjectDataProvider以返回所需的文本框数量)
1.
15
更新 然后,您可以使用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>
......
注意,我没有在样式上设置
x:Key
,因此默认情况下它将应用于网格中的所有文本框。如果您不希望设置x:Key
并将其用于所有文本框。正如HighCore在评论中提到的那样,ItemsControl
可能会对您有所帮助
创建一个ItemsControl,并将itemstemplate设置为包含文本框。(创建ObjectDataProvider以返回所需的文本框数量)
1.
15
更新 然后,您可以使用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>
......
注意,我没有在样式上设置
x:Key
,因此默认情况下它将应用于网格中的所有文本框。如果您不想设置x:Key
并将其用于所有文本框。您可以创建文本框的控件模板作为资源。将资源标记为x:Shared=False
<Grid.Resources>
<ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False">
<Grid.Resources>
在textbox的其他实例上使用此模板
<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>
您可以创建文本框的控件模板作为资源。将资源标记为x:Shared=False
<Grid.Resources>
<ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False">
<Grid.Resources>
在textbox的其他实例上使用此模板
<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>
您可以创建文本框的控件模板作为资源。将资源标记为x:Shared=False
<Grid.Resources>
<ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False">
<Grid.Resources>
在textbox的其他实例上使用此模板
<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>
您可以创建文本框的控件模板作为资源。将资源标记为x:Shared=False
<Grid.Resources>
<ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False">
<Grid.Resources>
在textbox的其他实例上使用此模板
<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>
#第一种方法:创建一个名为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>
在另一个窗口中添加
<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>
第一种方法:<