1.使用ListBox绑定Dictionary字典数据
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]; }
Xaml
<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模板
后台同上
Xaml定义:
<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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!