[英]Programmatically add MultiTrigger to ContentControl sub-class
一切正常,直到我將其稱為this.Triggers.Add(TriggerFocus)。 此類將ContentControl子類化。
這是我的代碼:
private void CreateTriggers()
{
TriggerFocus = new MultiTrigger
{
Conditions =
{
new Condition
{
Property = IsFocusedProperty,
Value = true
},
new Condition
{
Property = IsKeyboardFocusedProperty,
Value = true
}
},
Setters =
{
new Setter
{
Property = SetFocusProperty,
Value = true
},
}
};
this.Triggers.Add(TriggerFocus);
}
有任何想法嗎?
這是等效的xaml,它也會引發錯誤:
<ContentControl.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ContentControl.IsFocused" Value="True"/>
<Condition Property="ContentControl.IsKeyboardFocused" Value="True"/>
</MultiTrigger.Conditions>
<Setter TargetName="BorderFocus" Property="ContentControl.Visibility" Value="Visible" />
</MultiTrigger>
</ContentControl.Triggers>
經過一些測試,我將問題隔離到上面的MultiTrigger上。 如果將觸發器放置在模板或樣式中,這似乎很好用。
引發的錯誤不是很有幫助:System.Windows.Controls.ContentControl的初始化引發了異常
您只能將EventTriggers放入<ContentControl.Triggers>
集合中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.