私はテンプレートを持っています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 のみでこれを行う (ボタン クリックで選択した項目を更新する) 方法はありますか?