简体   繁体   English

以编程方式将MultiTrigger添加到ContentControl子类

[英]Programmatically add MultiTrigger to ContentControl sub-class

Everything works fine until I call this.Triggers.Add(TriggerFocus). 一切正常,直到我将其称为this.Triggers.Add(TriggerFocus)。 This class sub-classes ContentControl. 此类将ContentControl子类化。

Here's my code: 这是我的代码:

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);
    }

Any ideas? 有任何想法吗?

Here's the equivalent xaml which also throws an error: 这是等效的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>

After some testing I have isolated the problem to the MultiTrigger above. 经过一些测试,我将问题隔离到上面的MultiTrigger上。 It seems to work fine if I put the trigger in a template or style. 如果将触发器放置在模板或样式中,这似乎很好用。

The error thrown is not very informative: Initialization of 'System.Windows.Controls.ContentControl' threw an exception 引发的错误不是很有帮助:System.Windows.Controls.ContentControl的初始化引发了异常

您只能将EventTriggers放入<ContentControl.Triggers>集合中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM