DependencyProperty 回调方法未调用

发布于 2024-08-12 08:16:47 字数 1365 浏览 2 评论 0原文

我创建一个带有依赖属性(源)的 UserControl (TableWithFilter.xaml)。 UserControl 是一个表,其中包含不同项目的源属性。我创建了 XAML 并通过 XAML 绑定设置了源属性。到目前为止,一切都很好。

但是,如果依赖属性的值发生更改,则不会调用定义的回调方法。因此我无法更新表中的条目。有谁知道为什么不调用回调方法?

以下是“TableWithFilter”类中我的属性的定义:

Public Shared ReadOnly SourceProperty As DependencyProperty = _
        DependencyProperty.Register("Source", GetType(List(Of TableViewItem)), GetType(TableWithFilter), _
                                    New FrameworkPropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf TableWithFilter.ChangeSource)))

以及回调方法:

 Private Shared Sub ChangeSource(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim table As TableWithFilter = source
        table.Source = e.NewValue
    End Sub

以及此处的 XAML:

<Border Grid.Row="1" Grid.Column="1" BorderBrush="{StaticResource ElementBorder}" BorderThickness="1">
    <local:TableWithFilter x:Name="SearchResultTable" Source="{Binding Source={StaticResource contentFacade}, Path=ContentList}" />    
</Border>

如果属性“ContentList”更改,我预计将调用 TableWithFilder 类中的“ChangeSource”方法。但事实并非如此。更改 ContentList 属性后,我引发了以下事件:

RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ContentList"))

谢谢您的任何想法。

I create a UserControl (TableWithFilter.xaml) with a dependency property (source). The UserControl is a Table with a source property for the different items. I created the XAML and set the source property via the XAML Binding. So far so good.

But if the value of the dependency property is changed, the defined callback method is not called. Therefore I cannot update the entries in my table. Has anyone an idea why the callback method is not called?

Here is the definition of my property in the class "TableWithFilter":

Public Shared ReadOnly SourceProperty As DependencyProperty = _
        DependencyProperty.Register("Source", GetType(List(Of TableViewItem)), GetType(TableWithFilter), _
                                    New FrameworkPropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf TableWithFilter.ChangeSource)))

and the Callback method:

 Private Shared Sub ChangeSource(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim table As TableWithFilter = source
        table.Source = e.NewValue
    End Sub

and here the XAML:

<Border Grid.Row="1" Grid.Column="1" BorderBrush="{StaticResource ElementBorder}" BorderThickness="1">
    <local:TableWithFilter x:Name="SearchResultTable" Source="{Binding Source={StaticResource contentFacade}, Path=ContentList}" />    
</Border>

If the attribute "ContentList" is changed I expet that the "ChangeSource" method in the TableWithFilder class is called. But this is not the case. After I changed the ContentList attribute, I Raise the following Event:

RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ContentList"))

Thx for any ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞觅青森が 2024-08-19 08:16:47

整个属性的更改(即对指定集合的​​新引用)与列表本身的内容更改之间存在差异。集合有自己的更改通知界面,名为 INotifyCollectionChanged。因此,您需要做的是监听现在的属性更改,并在其中将 INotifyCollectionChanged 事件自己挂接到新的集合实例上,然后响应这些事件。

There's a difference between the entire property changing (i.e. a new reference to a collection being specified) vs. the contents of the list itself changing. Collections have their own change notification interface called INotifyCollectionChanged. So what you would need to do is listen for the property change you have now and, inside of that, hook the INotifyCollectionChanged events yourself on the new collection instance and then respond to those.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文