简体   繁体   English

如何在网格中放置一个 XAML 用户控件

[英]How to place an XAML usercontrol in a grid

I have the following main.xaml and usercontrol.我有以下 main.xaml 和用户控件。

I need to place several times the user control on the 2nd row, 2nd column of the grid, By using visual studio it wont allow to drag and drop the user control, so I suppose I have to do it by code, I just dont know how我需要将用户控件多次放置在网格的第 2 行第 2 列,通过使用 visual studio,它不允许拖放用户控件,所以我想我必须通过代码来完成,我只是不知道如何

MainPage.xaml主页.xaml

<Grid HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="1366" x:Name="grid" Background="Black">
        <Grid.RowDefinitions>
            <RowDefinition Height="150"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="250"/>
        </Grid.ColumnDefinitions>
        <Border BorderBrush="White" BorderThickness="3" Grid.Column="1" Background="Red" CornerRadius="30"/>
        <TextBlock x:Name="txtCountry" Grid.Column="1" TextWrapping="Wrap"  FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <TextBlock x:Name="txtTime" Grid.Row="1" TextWrapping="Wrap" FontSize="180" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>

Usercontrol用户控制

<UserControl
    x:Class="AlarmPro.TimeOnCity"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AlarmPro"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="150"
    d:DesignWidth="250">

    <Grid Background="Black">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border BorderBrush="#FFDE6A6A" BorderThickness="1" Grid.Row="0" Grid.Column="0" Background="#FFDC4646">
            <TextBlock TextWrapping="Wrap" Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16"/>
        </Border>
        <Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" Background="#FFAE4F00">
            <TextBlock TextWrapping="Wrap" Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36"/>
        </Border>

    </Grid>
</UserControl>

Do you mean like this? 你的意思是这样的吗?

 <my:UserControlName Grid.Column="2" Grid.Row="2" ... />

<my: in this case is the alias for the CLR namespace the UserControl resides in. It is defined at the top of your XAML, inside the <Window> or <UserControl> tag depending on context. <my:在这种情况下是UserControl所在的CLR命名空间的别名。它根据上下文定义在XAML的顶部,在<Window><UserControl>标记内。

For example, 例如,

<Window ... 
    xmlns:my="clr-namespace:AssemblyName"
    ...
/>

MainPage.Xaml MainPage.xaml中

<Page
    x:Class="UserControlExample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UserControlExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="MainContent" Background="Azure" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Hidden" 
                VerticalScrollBarVisibility="Hidden">                        
            <local:UserControl1 x:Name="MyHelloWorldUserControl" Grid.Row="1" />
        </ScrollViewer>
    </Grid>           
</Page>

UserControl1.xaml UserControl1.xaml

<UserControl
    x:Class="UserControlExample.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UserControlExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid Background="Bisque">
        <StackPanel>
            <StackPanel Orientation="Horizontal" Height="81">
                <TextBlock Text="Your Name is" Foreground="Blue" FontSize="30" Margin="0,0,0,10"/>
                <TextBox x:Name="Input" Background="White" Width="225" />
             </StackPanel>
             <Button Content="Click Me" Foreground="Brown" FontSize="30" Click="Button_Click"/>
             <TextBlock x:Name="Output" FontSize="100"/>
         </StackPanel>
     </Grid>
 </UserControl>

MainPage.xaml , I am binding a login UserControl by using the namespace : xmlns:UC="clr-namespace:Test.Views" , since I have my usercontrol in Folder named " Views ". MainPage.xaml ,我使用命名空间绑定登录UserControl:xmlns:UC =“clr-namespace:Test.Views”,因为我在名为“ Views ”的文件夹中有我的usercontrol。

<ScrollViewer>
<UC:Login BtnLoginClick="Login_BtnLoginClick"/>
</ScrollViewer>

Login.cs Login.cs

public partial class Login : UserControl    {

    public event EventHandler BtnLoginClick;

    public Login()
    {
        InitializeComponent();
    }

    private void btnLogin_Click(object sender, RoutedEventArgs e)
    {
        string userName = txtUserName.Text;
        string userPassword = txtUserPassword.Password.Trim();
        if (userName != null && userName != string.Empty && userPassword != null &&       userPassword != string.Empty)
        {
            if (this.BtnLoginClick != null)
            {
                this.BtnLoginClick(this, e);
            }
        }
        else
        {
            MessageBox.Show("Invalid username or password");
        }
    }

} }

Finally, dont forgot to use the event handler in MainPage.xaml to capture the button clicked event from Login Usercontrol to do other actions. 最后,不要忘记使用MainPage.xaml中的事件处理程序从Login Usercontrol捕获按钮单击事件以执行其他操作。

MainPage.xaml MainPage.xaml中

<UC:Login BtnLoginClick="Login_BtnLoginClick"/>

Here "BtnLoginClick" its an event Handler defined in the Login.xaml[User Control]. 这里“BtnLoginClick”是Login.xaml [User Control]中定义的事件处理程序。

Create new event for this "BtnLoginClick" event as i created "Login_BtnLoginClick". 当我创建“Login_BtnLoginClick”时,为此“BtnLoginClick”事件创建新事件。

MainPage.cs MainPage.cs

private void Login_BtnLoginClick(object sender, EventArgs e)
{
Messagebox.Show("Event captured successfully");
////Here you can add your stuffs...     
}

For UWP, in UserControl.xaml , find the local namespace xmlns:local notation: xmlns:local="using:ProjectName.Folder" at the top (by convention, C# namespaces are named the same way as the folder that contains them, so folder also means namespace ).对于 UWP,在UserControl.xaml中,找到本地命名空间xmlns:local notation: xmlns:local="using:ProjectName.Folder"在顶部(按照惯例,C# 命名空间的命名方式与包含它们的文件夹相同,因此文件夹也意味着命名空间)。

In MainPage.xaml , add a reference to this namespace.MainPage.xaml 中,添加对此命名空间的引用。 The reference prefix can be any name desired: for example, CustomPrefix , as in xmlns:CustomPrefix="using:ProjectName.Folder" .引用前缀可以是任何所需的名称:例如, CustomPrefix ,如xmlns:CustomPrefix="using:ProjectName.Folder"中所示。 Then, anywhere in MainPage.xaml , display the control by prefixing its name with <CustomPrefix:...> .然后,在MainPage.xaml中的任意位置,通过在其名称前加上前缀来显示控件<CustomPrefix:...>

UserControl.xaml UserControl.xaml

<UserControl
   xmlns:local="using:ProjectName.Folder">

MainPage.xaml主页.xaml

<Page
   xmlns:CustomPrefix="using:ProjectName.Folder">

   <CustomPrefix:UserControl />

</Page>

The project needs to be built to discard XAML errors in Visual Studio, otherwise the design view remains blank and the XAML has green squiggles.需要构建项目以丢弃 Visual Studio 中的 XAML 错误,否则设计视图将保持空白并且 XAML 会出现绿色波浪线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM