【发布时间】:2012-02-29 23:59:28
【问题描述】:
我正在尝试使用 DependencyProperty 进行绑定,但我什至无法让 DependencyProperty 工作,更不用说尝试绑定了。
我正在遵循 silverlight 指南,到目前为止,我应该能够使用 XAML 设置属性。这是我到目前为止的代码:
MainPage.xaml:
<UserControl
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"
xmlns:local="clr-namespace:UserControlSample" x:Class="UserControlSample.MainPage"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<local:InfoRectangle Margin="32,36,0,0" HorizontalAlignment="Left" Height="70" VerticalAlignment="Top" Width="122" InfoText="New Text"/>
<local:InfoRectangle Margin="105,139,188,97" InfoText="some text" />
</Grid>
InfoRectangle.xaml:
<UserControl
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"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
mc:Ignorable="d"
x:Class="UserControlSample.InfoRectangle"
d:DesignWidth="122" d:DesignHeight="70">
<Grid x:Name="LayoutRoot">
<Rectangle Fill="#FFABABE9" Stroke="Black" RadiusY="4" RadiusX="4"/>
<TextBlock Name="InfoLabel" Text="Text block" Margin="5" />
</Grid>
InfoRectangle.xaml.cs:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace UserControlSample
{
public partial class InfoRectangle : UserControl
{
public InfoRectangle()
{
// Required to initialize variables
InitializeComponent();
}
public string InfoText
{
get { return (string)GetValue(InfoTextProperty); }
set { SetValue(InfoTextProperty, value); }
}
public static readonly DependencyProperty InfoTextProperty =
DependencyProperty.Register(
"InfoText",
typeof(string),
typeof(InfoRectangle),
new PropertyMetadata("something", InfoTextChanged));
private static void InfoTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
}
}
当我运行解决方案时,会显示两个矩形,但它们只显示“文本块”,这既不是默认设置,也不是 MainPage XAML 中为用户控件设置的值。
【问题讨论】:
标签: silverlight dependency-properties