大佬们基本上都在讲怎么自定义控件,却怎么也没有说明怎么使用这些个自定义的控件,然后看完教程的本小白只能干瞪眼..可能大佬们不屑这样基础的东西 首先新建一个用户控件UserControl1.xaml,这个会用几下VS的应该都没有难度。
<UserControl x:Class="BlackBird.Control.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="600">
<Grid>
<!--此处省略代码,拷贝网络上的代码到这里,或者自己写-->
</Grid>
</UserControl>
然后是在需要添加该控件的窗体中的操作..
方法一:在xmal文件中添加
首先、要引用用户控件的命名空间 xmlns:bird="clr-namespace:BlackBird.Control"
<bird:UserControl1 x:Name="userControl1" />
然后、把用户控件添加到窗体中
<Window x:Class="WpfApplicationDemo.UserControlDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:bird="clr-namespace:BlackBird.Control"
Title="UserControlDemo" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<bird:UserControl1 x:Name="userControl11" />
</Grid>
</Window>
方法二:在cs代码中添加
比如我们把用户控件放到StackPanel面板中
<StackPanel Name="stackPanel1"></StackPanel>
在后台代码中,引用命名空间,实例化用户控件,添加到面板容器中即可
private void Window_Loaded(object sender, RoutedEventArgs e)
{
UserControl1 demo = new UserControl1();
this.stackPanel1.Children.Add(demo);
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有