Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让DependencyProperty在Silverlight中工作?_Silverlight_Dependency Properties - Fatal编程技术网

如何让DependencyProperty在Silverlight中工作?

如何让DependencyProperty在Silverlight中工作?,silverlight,dependency-properties,Silverlight,Dependency Properties,我正在尝试使用DependencyProperty进行绑定,但我甚至无法使DependencyProperty正常工作,更不用说尝试绑定到它了 我遵循silverlight指南,到目前为止,我应该能够使用XAML设置属性。以下是我目前掌握的代码: MainPage.xaml: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.micro

我正在尝试使用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>
当我运行解决方案时,会显示两个矩形,但它们只显示“文本块”,这既不是默认设置,也不是主页XAML中为用户控件设置的值

看来回调方法必须完成

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

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

我确实看到回调函数为空,但我认为问题是它甚至没有进入该函数。
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)
    {
    }
}
}
private static void InfoTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ((InfoRectangle)d).InfoLabel.Text = e.NewValue.ToString();
}