1

How would I add a behavior via a style in WPF?

Specifically, I need to add a behavior to a TreeViewItem via the ItemContainerStyle.

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <Style.Setters>
            <Setter Property="i:Interaction.Behaviors">
                <Setter.Value>
                    <behaviors:MyBehavior/>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>
</TreeView.ItemContainerStyle>

But at runtime, I get this:

System.Windows.Markup.XamlParseException: 'Set property 'System.Windows.Setter.Property' threw an exception.' Line number '422' and line position '34'. ---> System.ArgumentNullException: Value cannot be null. Parameter name: property at System.Windows.Setter.CheckValidProperty(DependencyProperty property) at......

There are also errors in the designer:

The property "Behaviors" is not a DependencyProperty. To be used in markup, non-attached properties must be exposed on the target type with an accessible instance property "Behaviors". For attached properties, the declaring type must provide static "GetBehaviors" and "SetBehaviors" methods.

RBT
  • 563
  • 1
  • 6
  • 15

0 Answers0