Reputation: 116
I am having trouble determining why my data is not being displayed. Here is my xaml.cs file. I left off the references as it compiles and runs fine.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string[] firstRecord = new string[3] {"1", "2", "3"};
listview1.Items.Add(firstRecord);
}
}
Here is the 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>
Upvotes: 0
Views: 1718
Reputation: 43596
If you have added an Array
to the ListView
your Column
bindings only need to be the index you want
<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>
Result:
However, you should be using DataBinding
to your ListView
as it is bad practice to access UI
controls in code in WPF
.
Here is an example of proper binding of your 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>
Code:
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; }
}
}
Result
Upvotes: 3
Reputation: 26058
Listviews aren't going to give you very good results using a string array. I would create an object and bind to that, something like this:
class Item
{
public string Item1 { get; set; }
public string Item2 { get; set; }
public string Item3 { get; set; }
}
then you can add to your UI like this:
listview1.Items.Add(new Item() { Item1 = "1", Item2 = "2", Item3 = "3" });
Then bind each column to the string property of your object:
<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>
Upvotes: 1
Reputation: 629
Your binding is wrong.
You bind for the first column:
{Binding Path=listview1.Items[0]}
which will contain {"1", "2", "3"} instead of "1" because {"1", "2", "3"} is the string-array which you add as first item to the items list.
Better would be
{Binding Path=listview1.Items[0][0]}
for the first, but this will always bind only your first list item.
But surely you see the fault now?
Maybe if you bind only
{Binding Path=listview1.Items}
the control knows what to do because the two-dimensional data will fit onto the listview's also two-dimensional data pattern. (But I didn't use this yet and don't know exactly.)
Upvotes: -1