Silverlight 3中的依赖项属性和数据上下文 [英] Dependency Properties and Data Context in Silverlight 3

查看:72
本文介绍了Silverlight 3中的依赖项属性和数据上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Silverlight 3 Beta,但遇到问题。我有一个页面,上面有我喜欢的用户控件。用户控件具有依赖项属性。如果用户控件未定义数据上下文(因此使用了父级的数据上下文),则一切正常。但是,如果用户控件具有自己的数据上下文,则永远不会调用依赖项属性的OnPropertyChanged方法。

I am working with Silverlight 3 beta, and am having an issue. I have a page that has a user control that I worte on it. The user control has a dependency property on it. If the user control does not define a data context (hence using the parent's data context), all works well. But if the user control has its own data context, the dependency property's OnPropertyChanged method never gets called.

以下是示例:

我的主页:

    <UserControl x:Class="TestDepProp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:app="clr-namespace:TestDepProp"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="100">
    <Grid x:Name="LayoutRoot" Background="White">
        <Border BorderBrush="Blue" BorderThickness="3" CornerRadius="3">
            <StackPanel Orientation="Horizontal">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="Enter text here:" />
                    <TextBox x:Name="entryBlock" Text="{Binding Data, Mode=TwoWay}"/>
                    <Button Content="Go!" Click="Button_Click" />
                    <TextBlock Text="{Binding Data}" />
                </StackPanel>
                <Border BorderBrush="Blue" BorderThickness="3" CornerRadius="3" Margin="5">
                    <app:TestControl PropOnControl="{Binding Data}" />
                </Border>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>

主页代码:

    using System.Windows;
using System.Windows.Controls;

namespace TestDepProp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            MainPageData data = new MainPageData();

            this.DataContext = data;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int i = 1;
            i++;
        }
    }
}

主页的数据上下文:

    using System.ComponentModel;

namespace TestDepProp
{
    public class MainPageData:INotifyPropertyChanged
    {

        string _data;
        public string Data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Data"));
            }
        }

        public MainPageData()
        {
            Data = "Initial Value";
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}

控制XAML:

    <UserControl x:Class="TestDepProp.TestControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:app="clr-namespace:TestDepProp"
    >
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Orientation="Vertical" Margin="10" >
            <TextBlock Text="This should change:" />
            <TextBlock x:Name="ControlValue" Text="Not Set" />
        </StackPanel>
    </Grid>
</UserControl>

控制代码:

    using System.Windows;
using System.Windows.Controls;

namespace TestDepProp
{
    public partial class TestControl : UserControl
    {
        public TestControl()
        {
            InitializeComponent();
            // Comment out next line for DP to work
            DataContext = new MyDataContext();
        }


        #region PropOnControl Dependency Property

        public string PropOnControl
        {
            get { return (string)GetValue(PropOnControlProperty); }
            set { SetValue(PropOnControlProperty, value); }
        }

        public static readonly DependencyProperty PropOnControlProperty =
                    DependencyProperty.Register("PropOnControl", typeof(string), typeof(TestControl), new PropertyMetadata(OnPropOnControlPropertyChanged));

        private static void OnPropOnControlPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TestControl _TestControl = d as TestControl;
            if (_TestControl != null)
            {
                _TestControl.ControlValue.Text = e.NewValue.ToString(); 
            }
        }
        #endregion PropOnControl Dependency Property

    }
}

控件的数据上下文:

    using System.ComponentModel;

namespace TestDepProp
{
    public class MyDataContext : INotifyPropertyChanged
    {

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}

到尝试一下,在文本框中键入内容,然后单击执行按钮。注释掉控件代码中的数据上下文,以确保它开始工作。

To try it out, type something in the text box, and hit the Go button. Comment out the data context in the controls code to see that it starts to work.

希望有人对发生的事情有个想法。

Hope someone has an idea as to what is going on.

推荐答案

用户控件的datacontext没有Data属性。

The user control's datacontext does not have a Data property.

因为它没有数据属性,所以数据绑定返回的null已经是默认值,因此该属性更改永远不会触发。

Because it doesn't have a data property the databinding returns null which is already the default value so the property change never fires.

这篇关于Silverlight 3中的依赖项属性和数据上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆