ListBoxItem DataTemplate with HyperLink Problem
-
I have a listbox bound to an entity called NavigationItemEntity which displays hyperlinks as the list items: XAML
<ListBox x:Name="itemsList"
ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}}"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Margin="2"
BorderBrush="Transparent"
BorderThickness="0"><ListBox.ItemTemplate> <DataTemplate> <TextBlock> <local:MaroisHyperlink LinkText="{Binding Caption}"> <i:Interaction.Triggers> <i:EventTrigger EventName="LinkClicked"> <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MaroisNavigationPane}}, Path=ItemClickedCommand}" CommandParameter="{Binding Path=SelectedItem,ElementName=itemsList}"/> </i:EventTrigger> </i:Interaction.Triggers> </local:MaroisHyperlink> </TextBlock> </DataTemplate> </ListBox.ItemTemplate>
</ListBox>
Code behind
private ICommand _ItemClickedCommand;
public ICommand ItemClickedCommand
{
get
{
if (_ItemClickedCommand == null)
_ItemClickedCommand = new RelayCommand<NavigationEntity>(p => ItemClickedExecuted(p), p => ItemClickedCanExecute(p));
return _ItemClickedCommand;
}
}private bool ItemClickedCanExecute(NavigationEntity args)
{
return true;
}private void ItemClickedExecuted(NavigationEntity args)
{
RaiseItemClickedEvent(args.Id, args.ItemType);
}The problem is that if I first don't click the list item, but just the link, then the link is sent as the command parameter. If I first click the list item, THEN click the link, then entity is passed, which is what I want. How can I force the list item to be selected when its link is clicked?
-
I have a listbox bound to an entity called NavigationItemEntity which displays hyperlinks as the list items: XAML
<ListBox x:Name="itemsList"
ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}}"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Margin="2"
BorderBrush="Transparent"
BorderThickness="0"><ListBox.ItemTemplate> <DataTemplate> <TextBlock> <local:MaroisHyperlink LinkText="{Binding Caption}"> <i:Interaction.Triggers> <i:EventTrigger EventName="LinkClicked"> <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MaroisNavigationPane}}, Path=ItemClickedCommand}" CommandParameter="{Binding Path=SelectedItem,ElementName=itemsList}"/> </i:EventTrigger> </i:Interaction.Triggers> </local:MaroisHyperlink> </TextBlock> </DataTemplate> </ListBox.ItemTemplate>
</ListBox>
Code behind
private ICommand _ItemClickedCommand;
public ICommand ItemClickedCommand
{
get
{
if (_ItemClickedCommand == null)
_ItemClickedCommand = new RelayCommand<NavigationEntity>(p => ItemClickedExecuted(p), p => ItemClickedCanExecute(p));
return _ItemClickedCommand;
}
}private bool ItemClickedCanExecute(NavigationEntity args)
{
return true;
}private void ItemClickedExecuted(NavigationEntity args)
{
RaiseItemClickedEvent(args.Id, args.ItemType);
}The problem is that if I first don't click the list item, but just the link, then the link is sent as the command parameter. If I first click the list item, THEN click the link, then entity is passed, which is what I want. How can I force the list item to be selected when its link is clicked?
When you click the link, it doesn't select the item first, so the
SelectedItem
won't be what you're expecting. Try using:CommandParameter="{Binding}"
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
When you click the link, it doesn't select the item first, so the
SelectedItem
won't be what you're expecting. Try using:CommandParameter="{Binding}"
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
That did it. Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.