如何在 XAML 中手动(无绑定)将 ListViewItem 添加到 WPF 多列 ListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12012721/
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
How to add ListViewItem to WPF multicolumn ListView by hand (no binding) in XAML
提问by heltonbiker
I have a problem with ListView design in Expression Blend that is harder than I thought it should.
我在 Expression Blend 中的 ListView 设计问题比我想象的要难。
I'd like to just draw a screen using XAML. This WILL NOT run inside an application, is just a static design study that should be rendered for viewing in the design window of Expression Blend, exclusively.
我只想使用 XAML 绘制一个屏幕。这不会在应用程序内运行,只是一个静态设计研究,应该在 Expression Blend 的设计窗口中专门呈现以供查看。
I have this so far:
到目前为止我有这个:
<ListView x:Name="examList" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" Width="Auto"/>
<GridViewColumn Header="Setup" Width="Auto"/>
<GridViewColumn Header="Protocol" Width="Auto" />
<GridViewColumn Header="Channels" Width="Auto"/>
<GridViewColumn Header="Duration" Width="Auto"/>
</GridView>
</ListView.View>
<ListViewItem>
<TextBlock Text="stuff" /> <!-- what should I put here??? -->
</ListViewItem>
</ListView>
The problem is: I don't have a clue how I should create ListViewItems with one string or number value for each field (each column in the GridView).
问题是:我不知道应该如何为每个字段(GridView 中的每一列)创建一个字符串或数字值的 ListViewItems。
I would appreciate any help, with or without code-behind, but a necessary requirement for my workflow is that it renders at design time inside Expression Blend without the need to run the application, and preferrably that the data is not bound from another file, but entered by hand directly into the XAML (I don't mind it, actually I WANT it).
无论是否有代码隐藏,我都会很感激任何帮助,但我的工作流程的一个必要要求是它在设计时在 Expression Blend 中呈现,而无需运行应用程序,并且最好不要从另一个文件绑定数据,但是直接手动输入到 XAML 中(我不介意,实际上我想要它)。
I found this answer, but it doesn't do what I need, I think.
我找到了这个答案,但我认为它没有做我需要的。
Thanks for reading!
谢谢阅读!
采纳答案by nemesv
You can use a string array inside your ListViewItemand use the DisplayMemberBindingto specify which which index should be displayed in which column.
您可以在内部使用字符串数组,ListViewItem并使用DisplayMemberBinding来指定哪个索引应显示在哪个列中。
<ListView x:Name="examList" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" Width="Auto"
DisplayMemberBinding="{Binding [0]}"/>
<GridViewColumn Header="Setup" Width="Auto"
DisplayMemberBinding="{Binding [1]}"/>
<GridViewColumn Header="Protocol" Width="Auto"
DisplayMemberBinding="{Binding [2]}"/>
<GridViewColumn Header="Channels" Width="Auto"
DisplayMemberBinding="{Binding [3]}" />
<GridViewColumn Header="Duration" Width="Auto"
DisplayMemberBinding="{Binding [4]}"/>
</GridView>
</ListView.View>
<ListViewItem>
<x:Array Type="{x:Type sys:String}">
<sys:String>This is Date</sys:String>
<sys:String>This is Setup</sys:String>
<sys:String>This is Protocol</sys:String>
<sys:String>This is Channels</sys:String>
<sys:String>This is Duration</sys:String>
</x:Array>
</ListViewItem>
</ListView>
Where sys:is xmlns:sys="clr-namespace:System;assembly=mscorlib"
哪里sys:是xmlns:sys="clr-namespace:System;assembly=mscorlib"
Or you can create your own datatype which will hold the values:
或者您可以创建自己的数据类型来保存值:
public class Exam
{
public string Date { get; set; }
public string Setup { get; set; }
//...
}
And use it in the ListViewItem:
并将其用于ListViewItem:
<ListView x:Name="examList" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" Width="Auto"
DisplayMemberBinding="{Binding Date}"/>
<GridViewColumn Header="Setup" Width="Auto"
DisplayMemberBinding="{Binding Setup}"/>
</GridView>
</ListView.View>
<ListViewItem>
<local:Exam Date="2001/1/1" Setup="Some setup" />
</ListViewItem>
</ListView>
Where local:point to your Examclass namespace.
当local:点到你的Exam类的命名空间。
回答by JazzSoft
You need index bindings like "[0]".
您需要像“[0]”这样的索引绑定。
<Grid>
<ListView x:Name="lv" />
</Grid>
lv.Items.Clear();
var gv = new GridView();
lv.View = gv;
var columns = new List<string> { "Date", "Setup", "Protocol", "Channels", "Duration" };
for(int index = 0; index < columns.Count; index++)
{
gv.Columns.Add(new GridViewColumn
{
Header = columns[index],
DisplayMemberBinding = new Binding("[" + index.ToString() + "]")
});
}
// Populate list
var row1 = new List<string> { "Date1", "Setup1", "Protocol1", "Channels1", "Duration1" };
var row2 = new List<string> { "Date2", "Setup2", "Protocol2", "Channels2", "Duration2" };
lv.Items.Add(row1);
lv.Items.Add(row2);

