I have a templated listbox:
<ListBox Grid.Row="0" Grid.Column="1" Background="Transparent" BorderThickness="0" x:Name="mainMenu"
ItemsSource="{Binding Source={x:Static local:MenuConfig.MainMenu}, Mode=OneTime}"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="PreviewMouseUp" Handler="SelectCurrentItem"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Button>
<StackPanel>
<Image Source="{Binding Icon}" MaxHeight="32" MaxWidth="32"/>
<TextBlock Text="{Binding Label}"/>
</StackPanel>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The selected item is updated manually with code behind:
private void SelectCurrentItem(object sender, MouseButtonEventArgs e)
{
ListBoxItem item = (ListBoxItem) sender;
item.IsSelected = true;
}
Is there a way to do this (update selected item on button click) with XAML only ?