我无法确定数据未显示的原因。这是我的xaml.cs文件。我把参考文献遗漏了,因为它编译并且运行正常。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string[] firstRecord = new string[3] {"1", "2", "3"};
listview1.Items.Add(firstRecord);
}
}
这是xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="400">
<Grid>
<ListView Name="listview1">
<ListView.View>
<GridView AllowsColumnReorder="true"
ColumnHeaderToolTip="Employee Information">
<GridViewColumn DisplayMemberBinding=
"{Binding Path=listview1.Items[0]}"
Header="Column1" Width="100"/>
<GridViewColumn DisplayMemberBinding=
"{Binding Path=listview1.Items[1]}"
Header ="2nd Column" Width="100">
</GridViewColumn>
<GridViewColumn DisplayMemberBinding=
"{Binding Path=listview1.Items[2]}"
Header="3rd Column" Width="100"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
答案 0 :(得分:2)
如果您已将Array
添加到ListView
Column
绑定,则只需要是您想要的索引
<ListView Name="listview1">
<ListView.View>
<GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Employee Information">
<GridViewColumn DisplayMemberBinding="{Binding Path=[0]}" Header="Column1" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=[1]}" Header ="2nd Column" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding Path=[2]}" Header="3rd Column" Width="100"/>
</GridView>
</ListView.View>
</ListView>
结果:
但是,您应该DataBinding
使用ListView
,因为访问UI
中代码中的WPF
控件是不好的做法。
以下是正确绑定String Arrays的示例
的Xaml:
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Name="UI">
<Grid>
<ListView ItemsSource="{Binding ElementName=UI, Path=Records}">
<ListView.View>
<GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Employee Information">
<GridViewColumn DisplayMemberBinding="{Binding Path=[0]}" Header="Column1" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=[1]}" Header ="2nd Column" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding Path=[2]}" Header="3rd Column" Width="100"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Records.Add(new string[3] { "1", "2", "3" });
Records.Add(new string[3] { "10", "20", "30" });
Records.Add(new string[3] { "100", "200", "300" });
}
private ObservableCollection<string[]> _records = new ObservableCollection<string[]>();
public ObservableCollection<string[]> Records
{
get { return _records; }
set { _records = value; }
}
}
结果
答案 1 :(得分:0)
使用字符串数组,Listviews不会给您带来非常好的结果。我会创建一个对象并绑定到它,如下所示:
class Item
{
public string Item1 { get; set; }
public string Item2 { get; set; }
public string Item3 { get; set; }
}
然后您可以像这样添加到您的用户界面:
listview1.Items.Add(new Item() { Item1 = "1", Item2 = "2", Item3 = "3" });
然后将每列绑定到对象的字符串属性:
<ListView Name="listview1">
<ListView.View>
<GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Employee Information">
<GridViewColumn DisplayMemberBinding="{Binding Item1}"
Header="Item1" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Item2}"
Header ="Item2" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Item3}"
Header="Item3" Width="100"/>
</GridView>
</ListView.View>
</ListView>
答案 2 :(得分:-1)
你的绑定是错误的。
您绑定第一列:
{Binding Path=listview1.Items[0]}
将包含{“1”,“2”,“3”}而不是“1” 因为{“1”,“2”,“3”}是作为第一项添加到项目列表中的字符串数组。
最好是
{Binding Path=listview1.Items[0][0]}
第一个是,但这只会绑定你的第一个列表项。
但你现在肯定看到了错误吗?
也许你只绑定
{Binding Path=listview1.Items}
控件知道该怎么做,因为二维数据将适合列表视图的二维数据模式。 (但我还没有使用它,也不完全确切。)