【问题标题】:How do I get DependencyProperty to work in Silverlight?如何让 DependencyProperty 在 Silverlight 中工作?
【发布时间】: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


    【解决方案1】:

    我在here 中的回答详细介绍了一个很好的紧凑示例,即依赖属性更新视图模型上的给定属性。您的视图模型上的属性将绑定到您的文本块,因此一旦更改通知触发,您的文本块应该更新。

    【讨论】:

      【解决方案2】:

      这里是解决方案;

      看来回调方法要完成了。

      private static void InfoTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          ((InfoRectangle)d).InfoLabel.Text = e.NewValue.ToString();
      }
      

      【讨论】:

      • 我确实看到回调是空的,但我认为问题在于它甚至没有进入。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      相关资源
      最近更新 更多