.net WPF 中的多列列表框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8911026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multicolumn ListBox in WPF
提问by user1156309
I have 3 TextBoxesand 1 Buttonand want to enter each of the the TextBoxesdata into a ListBoxin separate columns.
我有 3TextBoxes和 1 Button,想将每个TextBoxes数据输入到ListBox单独的列中。
I know how to enter data into one column:
我知道如何将数据输入一列:
listbox1.Items.Add(TextBox1.text);
but how can I enter the data into multiple columns?
但是如何将数据输入多列?
I am using .NET WPF. I want to use a ListBoxor a ListView.
我正在使用 .NET WPF。我想使用 aListBox或 a ListView。
回答by Ray
You want a ListViewinstead:
你想要一个ListView:
Something like this:
像这样的东西:
<ListView ItemsSource="{Binding SourceCollection}">
<ListView.View>
<GridView>
<GridViewColumn Header="Test1" DisplayMemberBinding="{Binding Test1}" />
<GridViewColumn Header="Test2" DisplayMemberBinding="{Binding Test2}" />
<GridViewColumn Header="Test3" DisplayMemberBinding="{Binding Test3}" />
<GridViewColumn Header="Button">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button>Button Text</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
回答by Dr. ABT
As Ray correctly points out, ListView will do the job. However if you're stuck with and/or want to use ListBox you can also use an ItemTemplate with Grid and set the Grid.IsSharedSizeScope propertyon the ListBox itself. For example:
正如 Ray 正确指出的那样,ListView 将完成这项工作。但是,如果您坚持使用和/或想要使用 ListBox,您还可以将 ItemTemplate 与 Grid 一起使用,并在 ListBox 本身上设置Grid.IsSharedSizeScope 属性。例如:
<ListBox ItemsSource="{Binding DataSource}" Grid.IsSharedSizeScope="True">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Column1"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="Column2"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="Column3"/>
</Grid.ColumnDefinitions>
<!-- Assumes MVVM and you wish to bind to properties and commands -->
<TextBlock Grid.Column="0" Text="{Binding ColumnOneText}"/>
<TextBlock Grid.Column="1" Text="{Binding ColumnTwoText}"/>
<TextBlock Grid.Column="2" Text="{Binding ColumnThreeText}"/>
<Button Content="ClickMe" Command="{Binding ButtonExecutionCommand}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This is a neat trick which can also be applied to other cases where you have multiple instances of Grid (ie: one per ListBoxItem as per this case) and want to share a column.
这是一个巧妙的技巧,它也可以应用于您有多个 Grid 实例(即:根据本例每个 ListBoxItem 一个)并想要共享一列的其他情况。
Best regards,
此致,
回答by Meysam Chegini
use groupbox and docpanel
使用 groupbox 和 docpanel
<GroupBox Width="250"
Margin="10,0,0,0"
FontSize="20"
Header="??????"
Style="{StaticResource Gb}">
<ListBox Margin="0" AlternationCount="2">
<ListBoxItem>
<Border Margin="0"
BorderBrush="#ddd"
BorderThickness="0,1,0,1">
<DockPanel Background="#f9f9f9" LastChildFill="True">
<controls2:TimeEditBox DockPanel.Dock="Right"
FontSize="13"
Mask="00:00"
Text="00:00"
TextAlignment="Center"
controls2:TextBoxMaskBehavior.Mask="Integer" />
<TextBox Width="50"
Margin="0,0,5,0"
DockPanel.Dock="Right"
FontSize="13"
Text="122"
TextAlignment="Center"
controls2:TextBoxMaskBehavior.Mask="Integer" />
<TextBlock VerticalAlignment="Center"
FontSize="13"
Text="????"
TextAlignment="Center" />
</DockPanel>
</Border>
</ListBoxItem>
**</ListBox>**
</GroupBox>

