日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF 用户控件的使用

發布時間:2025/3/21 asp.net 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF 用户控件的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

??? 我們來新建一個用戶控件UserControl1.xaml

<UserControl x:Class="WpfApplicationDemo.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="300" d:DesignWidth="300"><Grid><Border BorderThickness="3" CornerRadius ="5"Background="#FFFFCC" BorderBrush="#FF6633"><StackPanel Orientation="Vertical" Margin="5" HorizontalAlignment="Center"><Image Name="goodsImage" Height="80" Width="80" Margin="5"></Image><TextBlock Name="goodsPrice" Margin="5"></TextBlock><TextBlock Name="goodsQty" Margin="5"></TextBlock><Image Name="goodsBuy" Source="/images/fbxq_an.gif" Height="25" Width="25" Cursor="Hand" Margin="5"><Image.ToolTip>Add Quantity</Image.ToolTip></Image></StackPanel></Border></Grid></UserControl>

?在新建一個Window窗體,把用戶控件添加到Window窗體中

方法如下:

方法一:在xmal中添加

首先、要引用用戶控件的命名控件?xmlns:my="clr-namespace:WpfApplicationDemo.Control"

然后、把用戶控件添加到窗體中

<my:UserControl1 HorizontalAlignment="Left" Margin="38,46,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="183" Width="215" />

代碼如下:

<Window x:Class="WpfApplicationDemo.UserControlDemo"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:my="clr-namespace:WpfApplicationDemo.Control"Title="UserControlDemo" Height="300" Width="300" Loaded="Window_Loaded"><Grid><TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="下面是用戶控件" VerticalAlignment="Top" /><StackPanel Height="175" HorizontalAlignment="Left" Margin="20,57,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="246" /><my:UserControl1 HorizontalAlignment="Left" Margin="38,46,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="183" Width="406" /></Grid> </Window>

方法二:在cs代碼中添加

比如我們把用戶控件放到容器中

?<StackPanel Height="175" HorizontalAlignment="Left" Margin="20,57,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="246" ></StackPanel>?

然后在后臺代碼中,實例化用戶控件,添加到容器中即可

public partial class UserControlDemo : Window{public UserControlDemo(){InitializeComponent();}private void Window_Loaded(object sender, RoutedEventArgs e){UserControl1 demo = new UserControl1();this.stackPanel1.Children.Add(demo);}}

?

?

轉載于:https://www.cnblogs.com/shuang121/archive/2013/01/09/2853591.html

總結

以上是生活随笔為你收集整理的WPF 用户控件的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。