我已经为 WPF 创建了一个用户控件,例如 Windows 8 密码框模型。
我的 UserControl XAML 代码 -
<Grid.Resources>
<Style x:Key="ButtonWithoutHover" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
BorderThickness="0"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="Black" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Border BorderBrush="Black" BorderThickness="2" >
<DockPanel Canvas.Right="2" Canvas.Top="2">
<Button Style="{StaticResource ButtonWithoutHover}" Height="25" Width="20" BorderThickness="3" BorderBrush="White" DockPanel.Dock="Right" Background="CadetBlue" >
<Button.Content>
<Label Content="->" Foreground="White" />
</Button.Content>
</Button>
<TextBox Height="30" Width="180" FontSize="18" BorderThickness="0" Name="txtNumber" DockPanel.Dock="Left" >
</TextBox>
</DockPanel>
</Border>
编辑1:
我已经在我的项目中包含了这个用户控件。但是在 Wpf 应用程序中实现 UserControl 时,我的 UserControl 没有 Click 事件。所以我将 Click="UserControl1_Click" 添加到 Xaml。但它通过一个错误
XML 命名空间“clr-namespace:NumericTextbox;assembly=NumericTextbox”中不存在属性“Click”。
我的应用程序 Xaml 代码-
<Window x:Class="NumericAppication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:NumText="clr-namespace:NumericTextbox;assembly=NumericTextbox"
Title="MainWindow" Height="350" Width="525">
<Grid>
<NumText:UserControl1 Width="120" Height="30" Click="UserControl1_Click" />
</Grid>
</Window>