1. 程式人生 > 其它 >wpf 中如何引用自定義控制元件

wpf 中如何引用自定義控制元件

在這裡插入圖片描述
Ellis.yaml

<UserControl x:Class="WpfApplication.UserControlItems.Ellis"
             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:WpfApplication.UserControlItems" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <StackPanel>
<TextBlock Text="123"/> <TextBox /> </StackPanel> </UserControl>

main.yaml

<Window x:Class="WpfApplication.MainWindow"
        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" xmlns:local="clr-namespace:WpfApplication" mc:Ignorable="d" xmlns:ellis="clr-namespace:WpfApplication.UserControlItems" Title="MainWindow" Height="350" Width="525"> <Grid> <ellis:Ellis x:Name="test"/> </Grid> </Window>

首先需要在mainwindow.yaml裡新增自定義控制元件的引用(引用名稱空間即可)
xmlns:ellis=“clr-namespace:WpfApplication.UserControlItems”