C# 使用复选框在列表框中加载数据
我需要在带有复选框的列表框中加载一些数据(员工)。所以问题是如何加载其他值,因为此时只有我可以加载员工的“姓名”,并且我需要加载其他数据,如“姓氏”、“年龄”…等等 我不知道内容中的语法应该是怎样的 这是xamlC# 使用复选框在列表框中加载数据,c#,.net,wpf,xaml,listbox,C#,.net,Wpf,Xaml,Listbox,我需要在带有复选框的列表框中加载一些数据(员工)。所以问题是如何加载其他值,因为此时只有我可以加载员工的“姓名”,并且我需要加载其他数据,如“姓氏”、“年龄”…等等 我不知道内容中的语法应该是怎样的 这是xaml <ListBox.ItemTemplate> <DataTemplate> <CheckBox Name="CheckBoxZone" Content="{Binding Name}" Tag="{Binding TheValue}
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="CheckBoxZone" Content="{Binding Name}" Tag="{Binding TheValue}" Checked="CheckBoxZone_Checked"
Margin="0,5,0,0"/>
</DataTemplate>
</ListBox.ItemTemplate>
基本上,您必须增强ListItem模板的布局,以便与复选框一起显示其他控件:
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" Name="CheckBoxZone" Content="{Binding Name}" Tag="{Binding TheValue}" Checked="CheckBoxZone_Checked" Margin="0,5,0,0"/>
<TextBlock Grid.Column="1" Text="{Binding LastName}"></TextBlock>
</Grid>
</DataTemplate>
如果您想将项目本身传递给复选框的内容(为什么需要它?),只需使用以下命令:
<CheckBox Grid.Column="0" Name="CheckBoxZone" Content="{Binding}" Tag="{Binding TheValue}" Checked="CheckBoxZone_Checked" Margin="0,5,0,0"/>
要获得最大的灵活性,请对
复选框使用DataTemplate
<CheckBox Content="{Binding }">
<CheckBox.ContentTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Name}"/>
<Run Text="{Binding Family}" FontWeight="Bold"/>
<Run Text="{Binding Age, StringFormat='({0:d})'}" FontStyle="Italic"/>
</TextBlock>
</DataTemplate>
</CheckBox.ContentTemplate>
</CheckBox>
[假设年龄类型为int
,如果是双精度的,则使用{0:f}
]
这将导致类似这样的结果:MaikolSmith(36)
每个Run
标记都应该位于一个新行上,以便TextBlock
在它们之间插入一个空格。谢谢,只有一个问题,如何获得值之间的空格,例如:“Maikol Smith 36”。。。