我制作了以下示例用户控件:
<UserControl x:Class="DLSAdministration.Controls.CombinedTextBoxControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="30"
d:DesignWidth="250"
mc:Ignorable="d">
<UserControl.Resources>
<SolidColorBrush x:Key="BGBrush" Color="#3A3A3A" />
<SolidColorBrush x:Key="BorderBrush" Color="#656565" />
</UserControl.Resources>
<Grid Width="{Binding Path=Width, ElementName=tb}" Height="{Binding Path=Height, ElementName=tb}">
<TextBox Name="tb"
Grid.Row="0"
Grid.Column="0"
Background="{StaticResource BGBrush}"
BorderBrush="{StaticResource BorderBrush}"
BorderThickness="2"
FontFamily="Calibri"
FontSize="16"
Foreground="Snow">
Content
</TextBox>
<TextBlock Margin="0,5,5,5"
HorizontalAlignment="Right"
FontStyle="Italic"
Foreground="Red"
Padding="2">
Label
</TextBlock>
</Grid>
使用文件后面的代码:
public partial class CombinedTextBoxControl : UserControl
{
public CombinedTextBoxControl()
{
InitializeComponent();
}
// the exposed instance public properties.
public string TextboxText
{
get { return (string) GetValue(TextboxTextProperty); }
set { SetValue(TextboxTextProperty, value); }
}
public string TextBlockText
{
get { return (string)GetValue(TextBlockTextProperty); }
set { SetValue(TextBlockTextProperty, value); }
}
private static readonly DependencyProperty TextboxTextProperty =
DependencyProperty.Register
(
"TextboxText",
typeof (string),
typeof (CombinedTextBoxControl),
new FrameworkPropertyMetadata("TextboxText")
);
private static readonly DependencyProperty TextBlockTextProperty =
DependencyProperty.Register
(
"TextBlockText",
typeof(string),
typeof(CombinedTextBoxControl),
new FrameworkPropertyMetadata("TextBlockText ")
);
}
我的 UserControl 是这样使用的:
<my:CombinedTextBoxControl x:Name="combinedTextBoxControl1"
Grid.Column="1"
Width="182"
Margin="77,76,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
TextBlockText="Name"
TextboxText="Ibrar Mumtaz" />
我什至没有将任何数据绑定到此控件,我什至无法让它显示我通过 xaml 提供的一些示例数据?你能发现错误吗?我环顾四周,注意到其他人的错误,但我没有犯任何明显的错误,所以我完全被这个难住了....:S