Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Better to store the template in resources:</p> <pre><code>&lt;Window.Resources&gt; &lt;DataTemplate x:Key="DefaultTemplate"&gt; &lt;Grid x:Name="GridItem" Width="200"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock x:Name="Parameter" Grid.Column="1" Text="{Binding Path=Name}" Margin="5,1,0,0" /&gt; &lt;TextBlock x:Name="Value" Grid.Column="2" Text="{Binding Path=Age}" Margin="85,1,0,0" /&gt; &lt;Line x:Name="Separator" X1="0" X2="0" Y1="0" Y2="20" SnapsToDevicePixels="True" Grid.Column="1" Stroke="Black" StrokeThickness="2" Margin="50,0,0,0" HorizontalAlignment="Left" /&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; </code></pre> <p>ListBox define:</p> <pre><code>&lt;ListBox Name="MyListBox" ItemTemplate="{StaticResource DefaultTemplate}" /&gt; </code></pre> <p>In code C#:</p> <pre><code>public class Person { public string Name { get; set; } public int Age { get; set; } } </code></pre> <p>Define ObservableCollection:</p> <pre><code>private ObservableCollection&lt;Person&gt; MyListBoxData = new ObservableCollection&lt;Person&gt;(); </code></pre> <p>And add items on collection:</p> <pre><code>MyListBoxData.Add(new Person() { Name = "Nick", Age = 21, }); MyListBoxData.Add(new Person() { Name = "Adam", Age = 11, }); MyListBox.ItemsSource = MyListBoxData; </code></pre> <p><strong>EDITED:</strong></p> <p>Then set Width="3*", Width="*" and Margin="-WidthGrid" of the first TextBlock: </p> <pre><code>&lt;Grid x:Name="GridItem" Width="300"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="3*" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock x:Name="Parameter" Grid.Column="1" Text="{Binding Path=Name}" Margin="-220,0,0,0" /&gt; &lt;TextBlock x:Name="Value" Grid.Column="2" Text="{Binding Path=Age}" Margin="0,0,0,0" /&gt; &lt;/Grid&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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