Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this:</p> <pre><code>&lt;ListView x:Name="listViewUsers" SelectionMode="Single" ItemsSource="{Binding ElementName=window1, Path=Users, Mode=TwoWay}" MouseDoubleClick="listViewUsers_MouseDoubleClick"&gt; &lt;ListView.View&gt; &lt;GridView x:Name="gridViewUsers" AllowsColumnReorder="False"&gt; &lt;GridViewColumn&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;Image Source="{Binding Path=IsAdministrator, Converter={StaticResource boolToImage}, ConverterParameter='Images/admin18.gif|Images/user18.gif'}" /&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Header="User Name" DisplayMemberBinding="{Binding Path=UserName}" Width="140" /&gt; &lt;GridViewColumn Header="Full Name" DisplayMemberBinding="{Binding Path=FullName}" Width="140" /&gt; &lt;GridViewColumn Header="Phone Number" DisplayMemberBinding="{Binding Path=PhoneNumber}" Width="110" /&gt; &lt;GridViewColumn Header="Access Type" DisplayMemberBinding="{Binding Path=AccessType}" Width="110"&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;Image Cursor="Hand" ToolTip="Delete User" Stretch="None" Source="Images/trash12.gif" MouseUp="DeleteUser" /&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>Where in ItemsSource="{Binding ElementName=window1, Path=Users, Mode=TwoWay}"</p> <ul> <li><p>ElementName is the name of the Window in XAML (just add x:Name="window1" to the Window tag as with any other ontrol.</p></li> <li><p>Users is a List, should work the same with Dictionary</p></li> <li><p>Mode=TwoWay means that if the grid gets modified, the list will get modified too, and vice versa (Two way binding)</p></li> </ul> <p><strong>EDIT:</strong></p> <p>Try this:</p> <p>XAML:</p> <pre><code>&lt;ListView x:Name="listViewTest" ItemsSource="{Binding}"&gt; &lt;ListView.View&gt; &lt;GridView x:Name="gridViewTest"&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>C#:</p> <pre><code>public class TheClass { public int Col1, Col2, Col3; public Dictionary&lt;int, OtherColumns&gt; otherColumns = new Dictionary&lt;int,OtherColumns&gt;(); } public class OtherColumns { public string ColumnName; public int Value; } </code></pre> <p>And call this method under Window_Loaded:</p> <pre><code>private void PopulateListView() { TheClass c = new TheClass(); c.Col1 = 10; c.Col2 = 20; c.Col3 = 30; c.otherColumns.Add(0, new OtherColumns() { ColumnName = "Col4", Value = 40 }); c.otherColumns.Add(1, new OtherColumns() { ColumnName = "Col5", Value = 50 }); c.otherColumns.Add(3, new OtherColumns() { ColumnName = "Col6", Value = 60 }); DataTable table = new DataTable(); // adding regular columns table.Columns.Add("Col1", typeof(int)); table.Columns.Add("Col2", typeof(int)); table.Columns.Add("Col3", typeof(int)); // adding dynamic columns foreach (KeyValuePair&lt;int, OtherColumns&gt; pair in c.otherColumns) { table.Columns.Add(pair.Value.ColumnName, typeof(int)); } DataRow row = table.NewRow(); // adding regular column values to the DataTable row["Col1"] = c.Col1; row["Col2"] = c.Col2; row["Col3"] = c.Col3; // adding dynamic column values to the DataTable foreach (KeyValuePair&lt;int, OtherColumns&gt; pair in c.otherColumns) { row[pair.Value.ColumnName] = pair.Value.Value; } table.Rows.Add(row); // Start binding the table. gridViewTest.Columns.Clear(); System.Windows.Controls.GridViewColumn gvc; Binding binding; foreach (DataColumn column in table.Columns) { gvc = new System.Windows.Controls.GridViewColumn(); binding = new System.Windows.Data.Binding(); binding.Path = new PropertyPath(column.ColumnName); binding.Mode = BindingMode.OneWay; gvc.Header = column.Caption; gvc.DisplayMemberBinding = binding; gridViewTest.Columns.Add(gvc); } listViewTest.DataContext = table; } </code></pre> <p>I'm not saying it's the best solution, but it could help. Let me know.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload