如何放置一个XAML用户控件在网格 [英] How to place an XAML usercontrol in a grid

查看:180
本文介绍了如何放置一个XAML用户控件在网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我需要的地方多次在第二排的用户控件,网格的第2列,通过使用视觉工作室它不会允许拖放用户控件,所以我想我必须通过代码来做到这一点,我只是不知道如何



MainPage.xaml中

 <电网的Horizo​​ntalAlignment =左HEIGHT =768VerticalAlignment =评出的WIDTH =1366 X:NAME =网格背景=黑> 
< Grid.RowDefinitions>
< RowDefinition HEIGHT =150/>
< RowDefinition />
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition />
< ColumnDefinition WIDTH =250/>
< /Grid.ColumnDefinitions>
< BORDER = BorderBrush白了borderThickness =3Grid.Column =1=背景红CornerRadius =30/>
< TextBlock的X:名称=txtCountryGrid.Column =1TextWrapping =自动换行字号=36的Horizo​​ntalAlignment =中心VerticalAlignment =中心/>
< TextBlock的X:名称=txtTimeGrid.Row =1TextWrapping =自动换行字号=180的Horizo​​ntalAlignment =中心VerticalAlignment =中心/>
< /网格和GT;



用户控件



 <用户控件
X:类=AlarmPro.TimeOnCity
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:本地=使用:AlarmPro
的xmlns:D =HTTP: //schemas.microsoft.com/expression/blend/2008
的xmlns:MC =http://schemas.openxmlformats.org/markup-compatibility/2006
MC:可忽略=D
D:DesignHeight =150
D:DesignWidth =250>

<网格背景=黑>
< Grid.RowDefinitions>
< RowDefinition高度=30/>
< RowDefinition />
< /Grid.RowDefinitions>
将;边框BorderBrush =#FFDE6A6A了borderThickness =1Grid.Row =0Grid.Column =0的背景=#FFDC4646>
< TextBlock的TextWrapping =自动换行文本=的TextBlock的Horizo​​ntalAlignment =中心VerticalAlignment =中心字号=16/>
< /边框>
将;边框BorderBrush =黑了borderThickness =1Grid.Row =1背景=#FFAE4F00>
< TextBlock的TextWrapping =自动换行文本=的TextBlock的Horizo​​ntalAlignment =中心VerticalAlignment =中心字号=36/>
< /边框>

< /网格和GT;
< /用户控件>


解决方案

你的意思是这样吗?

 <我:UserControlName Grid.Column =2Grid.Row =2... /> 



<我的:在这种情况下别名为CLR命名空间中的用户控件驻留在它是在你的XAML的顶部定义的<内部;窗口> <用户控件方式> 标签根据上下文



例如,

 <窗口... 
的xmlns:我=CLR的命名空间:的AssemblyName
...
/>


I have the following main.xaml and usercontrol.

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

MainPage.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.

For example,

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

这篇关于如何放置一个XAML用户控件在网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆