我有一个模板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>
所选项目使用后面的代码手动更新:
private void SelectCurrentItem(object sender, MouseButtonEventArgs e)
{
ListBoxItem item = (ListBoxItem) sender;
item.IsSelected = true;
}
有没有办法仅使用 XAML 来执行此操作(单击按钮时更新所选项目)?