Usercontrol可以将已有的控件重新组合为新的控件,本文将会说明如何实现前台代码设置Usercontol的属性以及事件。
首先,我们先新建一个Usercontrol,命名为:UC
UC中,有一个Label和一个Button。
UC的 LabelText 属性与 Label 的 Content 绑定在一起。
UC的BTNClick属性与Button的Click事件关联在一起。
xaml代码:
<Grid >
<Label Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:UC}}, Path=LabelText}"/>
<Button Content="Button"
HorizontalAlignment="Left"
Margin="85,102,0,0" VerticalAlignment="Top"
Width="75"
Click="Button_Click"/>
</Grid>
cs代码:
public partial class UC : UserControl
{
public UC()
{
InitializeComponent();
}
#region Label
public static readonly DependencyProperty TitleProperty = DependencyProperty.R

本文介绍如何在WPF中创建UserControl并绑定属性和事件。通过示例展示了UserControl包含一个Label和Button,Label的Content与UserControl的LabelText属性绑定,Button的Click事件与UserControl的BTNClick属性关联。遇到Xaml报错问题,可通过运行程序或重启VS解决。

604

被折叠的 条评论
为什么被折叠?



