GetValue和SetValue是在托管代码中直接获取或设置对象属性的一种方法,如下面例子:
<UserControl x:Class="SilverlightApplication1.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas Height="312" HorizontalAlignment="Left" Name="canvas1" VerticalAlignment="Top" Width="400">
<Rectangle Canvas.Left="50" Canvas.Top="33" Height="47" Name="rectangle1" Stroke="Black" StrokeThickness="1" Width="238" />
</Canvas>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
ChangRect();
}
public void ChangRect()
{
double left =400;
double top =20;
double radius =20;
rectangle1.SetValue(Canvas.LeftProperty, left);
rectangle1.SetValue(Canvas.TopProperty, top);
rectangle1.SetValue(Rectangle.RadiusXProperty, radius);
rectangle1.SetValue(Rectangle.RadiusYProperty, radius);
}
}
}