1.使用ListBox綁定Dictionary字典數據html
ListBox經常使用事件SelectionChanged字體
private void bindListBox() { Dictionary<string, string> dic = new Dictionary<string, string>(); foreach (var item in Fonts.SystemFontFamilies.OrderBy(q => q.Source)) { dic.Add(item.Source, "---->" + string.Join(",", item.FamilyNames.Select(q => q.ToString()))); //dic.Add(item.Source,"------"); } listBox.ItemsSource = dic; } //選中結果事件 private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox thisBox = e.Source as ListBox; //e.AddedItems 全部選中的結果 //e.RemovedItems 全部未選中的結果 //解析結果是 Key Value鍵值對 KeyValuePair<string, string> item = (KeyValuePair<string, string>)e.AddedItems[0]; }
Xamlthis
<Grid> <Grid.RowDefinitions> <RowDefinition Height="57*"/> <RowDefinition Height="347*"/> </Grid.RowDefinitions> <ListBox x:Name="listBox" Grid.Row="1" SelectionChanged="listBox_SelectionChanged" /> <Label x:Name="label" Content="系統字體顯示" FontWeight="Bold" Foreground="Red" HorizontalAlignment="Left" Margin="36,22,0,0" VerticalAlignment="Top" Height="26" Width="97"/> </Grid>
2.使用字典集合單項綁定,ListBox.ItemTemplete模板spa
後臺同上code
Xaml定義:htm
<Grid> <Grid.RowDefinitions> <RowDefinition Height="21*"/> <RowDefinition Height="248*"/> </Grid.RowDefinitions> <ListBox x:Name="listBox" Grid.Row="1"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Label Grid.Column="0" Background="LightBlue" Content="{Binding Path=Key,Mode=OneWay}"/> <TextBox Grid.Column="1" Text="{Binding Path=Value,Mode=OneWay}"/> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
顯示結果:blog