title: How to add different ListBox items WPF
body: <p>I am implementing a chat application like this image:
  <a href="https://i.stack.imgur.com/bt3pT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bt3pT.png" alt="enter image description here"></a></p>
  
  <p>I started by creating a ListBox and setting a ListBox.ItemTemplate, but i can't figure out how can i control the ListBox to add an item with the layout as a message received or as send (just like Whatsapp).</p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;ListBox Name="ChatListBox" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="#00FFFFFF" BorderBrush="{x:Null}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"&gt;
                              &lt;ListBox.ItemsPanel&gt;
                                  &lt;ItemsPanelTemplate&gt;
                                      &lt;StackPanel Background="#00FFFFFF"/&gt;
                                  &lt;/ItemsPanelTemplate&gt;
                              &lt;/ListBox.ItemsPanel&gt;
                              &lt;ListBox.ItemContainerStyle&gt;
                                  &lt;Style TargetType="{x:Type ListBoxItem}"&gt;
                                      &lt;Setter Property="Focusable" Value="False"/&gt;
                                  &lt;/Style&gt;
                              &lt;/ListBox.ItemContainerStyle&gt;
                              &lt;ListBox.ItemTemplate&gt;
                                  &lt;DataTemplate&gt;
                                      &lt;Grid&gt;
                                          &lt;!--If the user sends a msg--&gt;
                                          &lt;Grid.ColumnDefinitions&gt;
                                              &lt;ColumnDefinition Width="9*"/&gt;
                                              &lt;ColumnDefinition Width="*"/&gt;
                                          &lt;/Grid.ColumnDefinitions&gt;
                                          &lt;Border Grid.Column="0" Margin="0" BorderThickness="1" BorderBrush="#9f9f9f" Background="#c4df9b" CornerRadius="10"&gt;
                                              &lt;TextBlock Name="MsgText" Background="#c4df9b" Foreground="Black" TextAlignment="Center" TextWrapping="Wrap" Margin="5" Text="{Binding text}" FontSize="14"/&gt;
                                          &lt;/Border&gt;
                                          &lt;Image Grid.Column="1" Source="Images/user.png" Margin="5" Height="{Binding ElementName=MsgText, Path=ActualHeight}"/&gt;
                                          &lt;!--
                                          If the user receives a msg
                                          &lt;Grid.ColumnDefinitions&gt;
                                              &lt;ColumnDefinition Width="*"/&gt;
                                              &lt;ColumnDefinition Width="9*"/&gt;
                                          &lt;/Grid.ColumnDefinitions&gt;
                                          &lt;Image Grid.Column="0" Source="Images/user.png" Margin="5" Height="{Binding ElementName=MsgText, Path=ActualHeight}"/&gt;
                                          &lt;Border Grid.Column="1" Margin="0" BorderThickness="1" BorderBrush="#9f9f9f" Background="#c4df9b" CornerRadius="10"&gt;
                                              &lt;TextBlock Name="MsgText" Background="#c4df9b" Foreground="Black" TextAlignment="Center" TextWrapping="Wrap" Margin="5" Text="{Binding text}" FontSize="14"/&gt;
                                          &lt;/Border&gt;--&gt;
                                      &lt;/Grid&gt;
                                  &lt;/DataTemplate&gt;
                              &lt;/ListBox.ItemTemplate&gt;
                          &lt;/ListBox&gt;
  </code></pre>
  
  <p>Here is my c# code:</p>
  
  <pre><code>List&lt;ChatItem&gt; chatItem = new List&lt;ChatItem&gt;();
  chatItem.Add(new ChatItem() { text = "Hello...", isFromUser = false });
          chatItem.Add(new ChatItem() { text = "hi!", isFromUser = true });
          chatItem.Add(new ChatItem() { text = "this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test", isFromUser = false });
          ChatListBox.ItemsSource = chatItem;
  </code></pre>
  
  <p>This is how the ListBox is redered:</p>
  
  <p><a href="https://i.stack.imgur.com/INDIt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/INDIt.png" alt="enter image description here"></a></p>
  
  <p>Is there any way of adding an IF statement at the ListBox with WPF? or how can i control which ListBox.ItemTemplate to add.</p>