Listboxを使う WPFListboxを使う WPFListBoxは基本のコントロール。しかし、ザムルのおかげで、結構複雑なコントロールにすることも可能なので、 ListView、DataGridなどと使い分けが必要だと思う。 見た目、軽さ、ソースの単純さなどから考慮されるのだろうと思います。 画像は、ザムルでリストアイテムを追加したものと、VBで追加したものと、データベースからバインドして追加したものの二つ。 バインドには、ItemTemplate 、DataTemplateというタグが、DataGridの時と同じように出てきます。 これはもう、コントロール内に、コントロールを入れ込む場合のXAMLのパターンとして覚えてしまったほうがよいようですね。 これって、実は、コントロールのContentに入れていることになるのかな? ![]()
<Window x:Class="ListBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListBox" Height="482" Width="300" WindowStartupLocation="CenterScreen">
<Grid>
<StackPanel>
<ListBox Width="200" Height="150" Name="list1">
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<!--画像を入れてみたもの-->
<Label Grid.Column="0" Content="北海道" Name="label1" />
<Image Grid.Column="1" Width="50" Name="Image2" VerticalAlignment="Top" Source="/WpfApplicationTips;component/Images/o0640048011914843315.jpg" />
</StackPanel>
</ListBoxItem>
<ListBoxItem Background="ForestGreen">5</ListBoxItem>
</ListBox>
<ListBox Width="200" Height="200" Name="list2">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding 県}" Margin="0,-5,0,-5" Padding="0,5,5,5"></Label>
<!--Marginとは lebalコントロール全体の余白-->
<!--Paddingとは、コントロール外周から、コンテンツ(テキストなど)までの余白-->
<!--因みに、バインドしないのListBoxと、バインドしたリストボックスでは、見た目が違うので、上記のMargin Paddingの設定が必要のようです-->
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Height="35" Name="Button1" />
</StackPanel>
</Grid>
</Window>
|