开发者

Silverlight & WPF - DataContext and the way that PropertyChangedEvents are KickedOff

Hello Stackoverflowians,

I have a questions in regards to what is the best way of making sure your Silverlight or WPF views are kicking off NotifyPropertyChanged events correctly.

I have a case in a Silverlight Application where I set my DataContext like this;

 public SubTopic SubTopicItem
        {
            get { return (SubTopic)GetValue(SubTopicItemProperty); }
            set { SetValue(SubTopicItemProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SubTopicItem.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SubTopicItemProperty =
            DependencyProperty.Register("SubTopicItem", typeof(SubTopic), typeof(AddModifySubTopic), new PropertyMetadata(null));


        public AddModifySubTopic()
        {
            InitializeComponent();
            this.DataContext = this;       
        }

And i am having problems with some Data Grids that the pull data out of Some EntityPropertys that are on this object.

My main questions is, would it make any real difference if instead of using

    public AddModifySubTopic开发者_JAVA百科()
    {
        InitializeComponent();
        this.DataContext = this;       
    }

i use:

        public AddModifySubTopic()
        {
            InitializeComponent();
            this.DataContext = SubTopicItem;       
        }

Would the Silverlight or WPF Framework treat the DataContext more directly, and Notify if any property or Entity Sets change through out the life time of the Object?

At the moment i am needing to update my DataGrids with a

DataGridName.SetValue(DataGrid.ItemsSourceProperty, SubTopicItem);

Thanks Everyone.

Cheers Robbie


It sounds like you are having problems with databinding. I think much of the code you posted isn't going to shed much light on the problem.

If binding isn't working, it is almost always a problem with:

  1. The XAML binding syntax.
  2. The source object and how INotifyPropertyChanged is implemented.

I would recommend distilling down the non-working code to make as simple a case as possible, and then post your XAML and your source object.

Edit

From your comment:

The problem occurs when a new item gets added to a List<TYPE> and the question is more asking about what is the best approach? this.DataContext = DEPENDENCYOBJECT or this.DataContext = CHILDWINDOW/PAGE etc.

How you set the DataContext should not affect how the binding operates (all other things being equal).

If you are really using a List<Type>, that is likely your problem, as collection change notifications work differently than property change notifications. You will want to bind to an ObservableCollection<Type>. I'm not sure if that's an option if your entity class is generated, but it might be (I'm not an Entity Framework expert).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜