ListBox With Hyperlinks - Pass Bound Item
WPF
1
Posts
1
Posters
3
Views
1
Watching
-
I have this listbox that displays its items as hyperlinks. The problem is that the click event is passing the hyperlink as the event args, not the bound NavigationEntity. What's the right way to do this?
<ListBox x:Name="listBox1"
ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}}"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Margin="2"
BorderBrush="Transparent"
BorderThickness="0"><ListBox.ItemTemplate> <DataTemplate> <TextBlock> <Hyperlink Foreground="SteelBlue" TextDecorations="Underline"> <TextBlock Text="{Binding Caption}"/> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ctrls:NavigationPane}}, Path=ItemClickedCommand}" PassEventArgsToCommand="True"/> </i:EventTrigger> </i:Interaction.Triggers> </Hyperlink> </TextBlock> </DataTemplate> </ListBox.ItemTemplate>
</ListBox>
The listbox is bound to a collection of NavigationEntity
public class NavigationEntity : _EntityBase
{
public int Id { get; set; }private string? \_Caption; public string Caption { get { return \_Caption; } set { if (\_Caption != value) { \_Caption = value; RaisePropertyChanged(nameof(Caption)); } } } private NavigationItemType \_ItemType; public NavigationItemType ItemType { get { return \_ItemType; } set { if (\_ItemType != value) { \_ItemType = value; RaisePropertyChanged(nameof(ItemType)); } } }
}
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.