IM使用了很多常用的文本框和标签,我的问题是如果 在wpf而不是复制粘贴?
例如,如果我有以下文本框,而是屏幕上的名称和位置 我希望所有文本框都有相同的行为
<TextBox x:Name="name2"
AcceptsReturn="True"
AllowDrop="True"
PreviewDragEnter="DropText_PreviewDragEnter"
PreviewDrop="DropText_PreviewDrop"
PreviewDragOver="DropText_PreviewDragOver"
HorizontalAlignment="Left" Height="20" TextWrapping="Wrap"
VerticalAlignment="Top" Width="80" Grid.Column="4" Margin="4,50,0,0" Grid.Row="2"
/>
<TextBox x:Name="name1"
AcceptsReturn="True"
AllowDrop="True"
PreviewDragEnter="DropText_PreviewDragEnter"
PreviewDrop="DropText_PreviewDrop"
PreviewDragOver="DropText_PreviewDragOver"
HorizontalAlignment="Left"
Height="20"
TextWrapping="Wrap"
Text="" VerticalAlignment="Top" Width="80" Grid.Column="4" Margin="4,75,0,0" Grid.Row="2"/>
答案 0 :(得分:1)
您可以使用Style
,它将存储控件的所有设置:
<Style TargetType="{x:Type TextBox}">
<Setter Property="AcceptsReturn" Value="True" />
<Setter Property="AllowDrop" Value="True" />
...
<Setter Property="Margin" Value="4,75,0,0" />
</Style>
如果样式定义了键,它将仅应用于它明确指示的控件。例如:
<Style x:Key="TextBoxOneStyle" TargetType="{x:Type TextBox}">
<Setter Property="AcceptsReturn" Value="False" />
<Setter Property="AllowDrop" Value="True" />
...
<Setter Property="Margin" Value="4,0,0,0" />
</Style>
<Style x:Key="TextBoxTwoStyle" TargetType="{x:Type TextBox}">
<Setter Property="AcceptsReturn" Value="True" />
<Setter Property="AllowDrop" Value="True" />
...
<Setter Property="Margin" Value="4,75,0,0" />
</Style>
使用:
<TextBox Name="TextBoxOne"
Style="{StaticResource TextBoxOneStyle}" />
<TextBox Name="TextBoxTwo"
Style="{StaticResource TextBoxTwoStyle}" />
您还可以通过EventSetter
指定事件处理程序:
<Style TargetType="{x:Type TextBox}">
<EventSetter Event="PreviewDragEnter" Handler="DropText_PreviewDragEnter" />
</Style>
请参阅此链接,了解更多信息: