I have defined the following behavior for ListBoxItem
s :
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 ListViewItem
s 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?