简体   繁体   中英

Programmatically add MultiTrigger to ContentControl sub-class

Everything works fine until I call this.Triggers.Add(TriggerFocus). This class sub-classes 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:

<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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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