XAML代码设置附加属性

这里介绍在C#代码中动态添加控件的时候设置附加属性的两种方法。

首先来XAML:

<phone:PhoneApplicationPage
    x:Class="Button.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">


    <Grid Name="root">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.2*"></RowDefinition>
            <RowDefinition Height="0.3*"></RowDefinition>
            <RowDefinition Height="0.5*"></RowDefinition>
        </Grid.RowDefinitions>
    </Grid>

</phone:PhoneApplicationPage>

我要在Grid里面动态添加一个TextBlock

方法一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Button.Resources;

namespace Button
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            TextBlock tb = new TextBlock();
            tb.Text = "New Tb";
            //Grid.SetColumn(tb, 1);
            //Grid.SetRow(tb, 2);

            tb.SetValue(Grid.RowProperty, 1);
            tb.SetValue(Grid.ColumnProperty, 1);
            
            root.Children.Add(tb);
        }

        
    }
}

直接用SetValue函数设置,附加属性是Grid.RowProperty和Grid.ColumnProperty


方法二:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Button.Resources;

namespace Button
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            TextBlock tb = new TextBlock();
            tb.Text = "New Tb";
            Grid.SetColumn(tb, 1);
            Grid.SetRow(tb, 2);

            //tb.SetValue(Grid.RowProperty, 1);
            //tb.SetValue(Grid.ColumnProperty, 1);
            
            root.Children.Add(tb);
        }

        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值