6

我有以下 main.xaml 和用户控件。

我需要将用户控件多次放置在网格的第 2 行第 2 列,通过使用 Visual Studio,它不允许拖放用户控件,所以我想我必须通过代码来完成,我只是不知道如何

主页.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
    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>
4

3 回答 3

11

你的意思是这样吗?

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

<my:在这种情况下,是 CLR 命名空间的别名UserControl。它定义在 XAML 的顶部,在<Window>or<UserControl>标记内,具体取决于上下文。

例如,

<Window ... 
    xmlns:my="clr-namespace:AssemblyName"
    ...
/>
于 2012-06-10T22:18:50.707 回答
5

主页.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

<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>
于 2013-08-27T04:41:58.500 回答
2

MainPage.xaml,我正在使用命名空间绑定登录用户控件: xmlns:UC="clr-namespace:Test.Views" ,因为我的用户控件位于名为“ Views ”的文件夹中。

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

登录.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");
        }
    }

}

最后,不要忘记使用 MainPage.xaml 中的事件处理程序来捕获来自 Login Usercontrol 的按钮单击事件以执行其他操作。

主页.xaml

<UC:Login BtnLoginClick="Login_BtnLoginClick"/>

这里“BtnLoginClick”是一个在 Login.xaml[User Control] 中定义的事件处理程序。

当我创建“Login_BtnLoginClick”时,为此“BtnLoginClick”事件创建新事件。

主页.cs

private void Login_BtnLoginClick(object sender, EventArgs e)
{
Messagebox.Show("Event captured successfully");
////Here you can add your stuffs...     
}
于 2013-11-06T06:59:52.990 回答