public register() { InitializeComponent(); }
时间: 2024-02-10 15:08:44 浏览: 27
这段代码是C#中的一个构造函数。具体来说,这是一个名为register的构造函数,没有参数,且访问修饰符为public。在构造函数中,调用了一个名为InitializeComponent的方法,该方法会初始化该类的组件。在WPF应用程序中,组件通常是指XAML中定义的控件,如按钮、文本框等。InitializeComponent方法会将XAML中定义的控件实例化,并将其添加到该类的组件树中。通过调用构造函数,可以创建该类的一个实例,并且该实例的组件已经被正确地初始化,可以在程序中使用。
相关问题
自定义控件定义一个Border的外部属性,要求类型为Border,外部Border传入时才展示Border public partial class CustomFont : ItemsControl, INotifyPropertyChanged { public CustomFont() { InitializeComponent(); } private string title2 = string.Empty; /// <summary> /// 展示文字 /// </summary> public string Title2 { get { return title2; } set { title2 = value; OnPropertyChanged("Title2"); } } private bool showUnderline = false; // ... public bool ShowUnderline { get { return showUnderline; } set { showUnderline = value; OnPropertyChanged("ShowUnderline"); } } private double characterSpacing = 0; /// <summary> /// 字符间距 /// </summary> public double CharacterSpacing { get { return characterSpacing; } set { characterSpacing = value; OnPropertyChanged("CharacterSpacing"); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
如果你想在自定义控件中定义一个 Border 类型的属性,可以在自定义控件的类中添加一个名为 Border 的属性,并在属性的 Set 方法中进行相应的操作。具体实现代码如下:
```
public partial class CustomControl : Control
{
public static readonly DependencyProperty BorderProperty =
DependencyProperty.Register("Border", typeof(Border), typeof(CustomControl), new PropertyMetadata(null));
public Border Border
{
get { return (Border)GetValue(BorderProperty); }
set { SetValue(BorderProperty, value); }
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (Border != null)
{
Border.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Border.Arrange(new Rect(new Point(0, 0), Border.DesiredSize));
drawingContext.DrawRectangle(Border.Background, new Pen(Border.BorderBrush, Border.BorderThickness), new Rect(0, 0, Border.DesiredSize.Width, Border.DesiredSize.Height));
}
}
}
```
在 XAML 中使用该自定义控件时,可以在外部定义一个 Border 控件,并将它赋值给 CustomControl 的 Border 属性,如下所示:
```
<Window ...>
<Grid>
<Border x:Name="myBorder" BorderThickness="2" BorderBrush="Red">
<TextBlock Text="Hello, World!" />
</Border>
<local:CustomControl Border="{Binding ElementName=myBorder}" />
</Grid>
</Window>
```
这样,在 CustomControl 中就可以显示外部定义的 Border 了。注意,这里使用了数据绑定,需要在 CustomControl 的代码中实现 INotifyPropertyChanged 接口。
xaml代码:<ItemsControl x:Class="HDSProject.Control.CustomFont" 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" xmlns:local="clr-namespace:HDSProject.Control" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" x:Name="itemCtrl" ItemsSource="{Binding MyProperty, RelativeSource={RelativeSource AncestorType={x:Type local:CustomFont}}}" FontSize="20" Foreground="Teal" > <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border Margin="2"> <ContentPresenter Content="{Binding}"/> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>后台代码:public partial class CustomFont : ItemsControl { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register( "MyProperty", typeof(string), typeof(CustomFont), new PropertyMetadata("Default Value")); public CustomFont() { InitializeComponent(); } public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } private string title2 = "测试字体间距zjis"; public string Title2 { get { return title2; } set { title2 = value; OnPropertyChanged("Title2"); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }调用处:<Control:CustomFont MyProperty="islearner"/>
这段代码实现了一个自定义控件 CustomFont,它继承自 ItemsControl。通过设置 ItemsSource 属性绑定到 CustomFont 控件的 MyProperty 属性,可以将 MyProperty 中的数据展示为一个个带边框的内容项。ItemsPanelTemplate 指定了内容项排列方式为水平方向的 StackPanel。ItemTemplate 指定了每个内容项的展示方式为一个带边框的 ContentPresenter。后台代码中定义了 MyProperty 属性和 Title2 属性,其中 MyProperty 是一个依赖属性,可以通过 SetValue 和 GetValue 方法设置和获取其值。OnPropertyChanged 方法用于通知属性变化。最后,在调用处使用 Control:CustomFont 标签,并通过 MyProperty 属性设置 CustomFont 控件的数据源。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)