本文介绍了WPF:PropertyChangedCallback触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,它公开了一个的DependencyProperty称为VisibileItems每当该属性得到更新,我需要触发另一个事件。为了实现这个目标,我加入PropertyChangedCallback事件FrameworkPropertyMetadata。

I have a user control, which exposes a DependencyProperty called VisibileItemsEvery time that property gets updated, i need to trigger another event.To achieve that, i added a FrameworkPropertyMetadata with PropertyChangedCallback event.

由于某些原因,这个事件被调用一次,而不会触发下一次VisibleItems改变。

For some reason, this event gets called only once, and doesn't trigger the next time VisibleItems is changed.

XAML:

<cc:MyFilterList VisibleItems="{Binding CurrentTables}"  />

CurrentTables是MyViewModel一个的DependencyProperty。 CurrentTables被经常改变。我可以绑定另一个WPF控件CurrentTables,而我看到的变化在用户界面中。

CurrentTables is a DependencyProperty on MyViewModel. CurrentTables gets changed often. I can bind another WPF control to CurrentTables, and i see the changes in the UI.

下面是我的有线VisibleItems与PropertyChangedCallback的方式

Here is the way i wired VisibleItems with PropertyChangedCallback

public static readonly DependencyProperty VisibleItemsProperty =
    DependencyProperty.Register(
    "VisibleItems",
    typeof(IList),
    typeof(MyFilterList),
    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(VisiblePropertyChanged))

    );

public IList VisibleItems {
    get { return (IList)GetValue(VisibleItemsProperty); }
    set { SetValue(VisibleItemsProperty, value); }
}

由踏入VisiblePropertyChanged,我可以看到它被触发第一次CurrentTables获取设置。但随后的时间。

by stepping into VisiblePropertyChanged, i can see that it gets triggered the first time CurrentTables gets set. but not subsequent times.

更新

因为你们当中有些人质疑CurrentTables被修改的方式,它完全是在变化的重新分配:

as some of you questioned the way CurrentTables is modified, it is re-assigned completely on change:

OnDBChange()...
CurrentTables = new List<string>(MainDatabaseDataAdapter.GetTables(this.SelectedServer, this.SelectedDatabase));

这条线被调用的每一个变化,但我VisiblePropertyChanged处理程序被调用仅在第一次。

this line gets called on every change, but my VisiblePropertyChanged handler gets called only the first time.

更新

如果我直接分配VisibleItems,该处理程序被调用每一次!

if i assign VisibleItems directly, the handler does get called every time!

TestFilterList.VisibleItems = new List<string>( Enumerable.Range(1, DateTime.Now.Second).ToList().Select(s => s.ToString()).ToList() );

所以,它看起来与的DependencyProperty(VisibleItems)之类的问题的根源看另外的DependencyProperty(CurrentTables)。不知怎的,在第一个属性更改绑定的作品,而不是在后来的?试图与窥探检查这个问题,因为你们中的一些建议。

So, it looks like the problem stems from the DependencyProperty (VisibleItems) watching another DependencyProperty (CurrentTables). Somehow the binding works on first property change, but not on subsequent ones?Attempting to inspect this issue with snoop as some of you suggested.

推荐答案

如果你只是实例化一个 MyFilterList ,并设置 VisibleItems 通过code是这样的:

If you just instantiate a MyFilterList and set VisibleItems via code like this:

var control = new MyFilterList();
control.VisibleItems = new List<string>();
control.VisibleItems = new List<string>();

您可能会看到PropertyChangedCallback发生的每一次。意,问题是与结合,不回调。请确保你没有绑定错误,你是在养的PropertyChanged ,而你没有违反的结合(例如,通过设置 VisibleItems 在code)

You will likely see the PropertyChangedCallback happen every time. Meaning, the problem is with the binding, not the callback. Make sure you don't have binding errors, you're raising PropertyChanged, and you're not breaking the binding (e.g. by setting VisibleItems in code)

这篇关于WPF:PropertyChangedCallback触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 08:48