Skip to content

Instantly share code, notes, and snippets.

@hilapon
Last active August 29, 2015 14:13
Show Gist options
  • Save hilapon/ab1791cec586bdb61ac6 to your computer and use it in GitHub Desktop.
Save hilapon/ab1791cec586bdb61ac6 to your computer and use it in GitHub Desktop.
[WPF] 項目を Button で表示する ListBox のサンプル
<!-- MainWindow.xaml -->
<Window x:Class="MenuButtonApp.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
xmlns:v="clr-namespace:MenuButtonApp2.Views"
xmlns:vm="clr-namespace:MenuButtonApp2.ViewModels"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<ListBox ItemsSource="{Binding Persons}" SelectedItem="{Binding Person}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}" Width="300"
Command="{Binding DataContext.MessageCommand,
RelativeSource={RelativeSource AncestorType=Window}}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment