0

I have defined the following behavior for ListBoxItems :

using System.Windows.Controls;
using System.Windows.Interactivity;

namespace MyBehavior {
    public partial class ListBoxSelectableBehavior : Behavior<ListBoxItem> {
        protected override void OnAttached( ) => base.OnAttached( );
    }
}

( Obviously there is more to it than this, but because MCVE, and that's not relevant to my question, I'm just presenting the relevant portion. Also, since ListViewItem inherits ListBoxItem directly, it's valid for either case. )

I need to know how I can take the above defined behavior and attach it to ListViewItems generated by the following :

<Window
    x:Class="MCVE.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MCVE"
    xmlns:lib="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <x:Array x:Key="ListViewSource" Type="{x:Type lib:String}">
            <lib:String>Foo</lib:String>
            <lib:String>Bar</lib:String>
            <lib:String>Baz</lib:String>
        </x:Array>
    </Window.Resources>
    <ListView ItemsSource="{StaticResource ListViewSource}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Window>

I know that to attach behaviors to elements, you need to import the System.Windows.Interactivity namespace, and define it in XAML, and the XAML would be...

<myFoo:Bar>
    <i:Interaction.Behaviors>
        <!-- Behaviors Go Here -->
    </i:Interaction.Behaviors>
</myFoo:Bar>

What I don't know is where I would put that to have it affect the ListView generated items.

So... how do I get my defined behavior to affect my ListView generated items?

Will
  • 3,413
  • 7
  • 50
  • 107
  • 1
    ListView.ItemContainerStyle – Dave M Mar 17 '18 at 00:56
  • @DaveM So... what? I just plop the XAML directly in the style? – Will Mar 17 '18 at 01:07
  • Either: https://stackoverflow.com/questions/1647815/ or implement your behavior as an AttachedProperty instead (one reason I prefer to implement “behaviors” as AttachedProperties) – Dave M Mar 17 '18 at 01:23

0 Answers0