DependencyProperty 依赖项和 PropertyCallbacks
这篇文章的核心问题是,您是否可以期望在设置其中一个 DP 的属性回调时设置所有 DP。我问这个是因为这不是我看到的行为。
一个类有两个 DP,这两个 DP 都是在 XAML 中设置的,如下所示:
<!-- Days of the Week -->
<local:DayOfTheWeekColumn
DowIndex="0"
ActivityCollection="{Binding Source={StaticResource spy}, Path=DataContext}"
....
/>
在 DayOfTheWeekColumn 类中,暂时如下声明 DP:
public static readonly DependencyProperty DowIndexProperty = DependencyProperty.RegisterAttached(
"DowIndex", typeof(string), typeof(DayOfTheWeekColumn),
new PropertyMetadata(OnDowIndexSet), IsIndexValid);
public static readonly DependencyProperty ActivityCollectionProperty = DependencyProperty.RegisterAttached(
"ActivityCollection", typeof(IActivityCollectionViewModelBase), typeof(DayOfTheWeekColumn),
new PropertyMetadata(OnActivityCollectionSet));
当执行 OnDowIndexSet 回调时,ActivityCollectionProperty 仍为 null,但当执行 OnActivityCollectionSet 回调时,DowIndexProperty 被估值。我需要这两个属性来完成这个用例。这是 OnActivityCollectionSet:
private static void OnActivityCollectionSet(DependencyObject target, DependencyPropertyChangedEventArgs e) {
var context = (IActivityCollectionViewModelBase) e.NewValue;
var col = (DayOfTheWeekColumn) target;
var index = Convert.ToInt32(col.DowIndex);
_setHeader(col, context, index);
}
现在这可以工作,但只要我不了解在执行回调时设置这两个属性的时间,它对我来说就很脆弱。为什么这两个属性都可用于 OnActivityCollectionSet 而不是 OnDowIndexSet?
干杯,
贝里尔
The heart of the question in this post is whether you can expect all DPs to be set by the time a Property callback for one of them is set. I ask this because this is not the behavior I am seeing.
A class has two DP's, both of which are set in XAML, like so:
<!-- Days of the Week -->
<local:DayOfTheWeekColumn
DowIndex="0"
ActivityCollection="{Binding Source={StaticResource spy}, Path=DataContext}"
....
/>
In the DayOfTheWeekColumn class, the DPs are declared like so, for now:
public static readonly DependencyProperty DowIndexProperty = DependencyProperty.RegisterAttached(
"DowIndex", typeof(string), typeof(DayOfTheWeekColumn),
new PropertyMetadata(OnDowIndexSet), IsIndexValid);
public static readonly DependencyProperty ActivityCollectionProperty = DependencyProperty.RegisterAttached(
"ActivityCollection", typeof(IActivityCollectionViewModelBase), typeof(DayOfTheWeekColumn),
new PropertyMetadata(OnActivityCollectionSet));
When the OnDowIndexSet callback executes, the ActivityCollectionProperty is still null, but when the OnActivityCollectionSet callback executes, the DowIndexProperty is valued. I need both properties to accomplish this use case. Here is OnActivityCollectionSet:
private static void OnActivityCollectionSet(DependencyObject target, DependencyPropertyChangedEventArgs e) {
var context = (IActivityCollectionViewModelBase) e.NewValue;
var col = (DayOfTheWeekColumn) target;
var index = Convert.ToInt32(col.DowIndex);
_setHeader(col, context, index);
}
Now this works, but it is fragile to me as long as I don't understand the timing of setting both properties by the time the callbacks execute. Why should both properties be available for OnActivityCollectionSet and not OnDowIndexSet?
Cheers,
Berryl
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许在 XAML 中的 ActivityCollection 之前设置 DowIndex 之前?
您不能使用默认的 DependencyProperty 值来防止此问题吗?
小提示:Path 绑定属性是默认属性,因此您可以使用以下较短的表示法:
Maybe before the DowIndex is set before ActivityCollection in XAML?
And couldn't you use default DependencyProperty value to prevent this issue?
And small OT tip: The Path binding property is a default one so you could use this shorter notation: