title: How to prevent ListView from Cutting off Items
body: <p>I'm using a ListView box that is extending a custom property, ListViewcolumns.Stretch. This is so I can have 3 columns that space themselves evenly regardless of Window size. My problem is at the end of the listview items are being "cut off"</p>
  
  <p>So my listview contains 3 columns, Red, Green, and Black, when I insert an item it inserts into the appropriate column and the other two columns inserts blank text. It inserts the items at the 0 location so it always shows on top and pushes everything else down. The scrollbar is by default off.</p>
  
  <p>-- Goal is to have 3 columns take up the entire window space regardless of how big or small the window is and resize the text appropriately (max height around 100-150). AND then when rows fill up the entire collection the bottom row cannot "span" out of view, it should either be all in view or none in view.</p>
  
  <p>Anyways - the codes. Any help would be appreciated, I could hard code the maxheight of the viewbox until I found the perfect size that works on my target monitor but ideally I want it to resize appropriately</p>
  
  <p><strong>ListView</strong></p>
  
  <pre><code>&lt;ListView Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent" Margin="15,10,5,5"
                        FontFamily="Impact" FontWeight="Bold" BorderThickness="0" VerticalContentAlignment="Stretch"
                    ScrollViewer.VerticalScrollBarVisibility="{Binding VerticleScrollVisibility}" ScrollViewer.HorizontalScrollBarVisibility="Auto"
                    ItemsSource="{Binding PlayNumbers}"
                    ItemContainerStyle="{StaticResource ListViewStyle}" ext:ListViewColumns.Stretch="True"&gt;
                  &lt;ListView.View&gt;
                      &lt;GridView&gt;
                          &lt;GridView.ColumnHeaderContainerStyle&gt;
                              &lt;Style&gt;
                                  &lt;Setter Property="FrameworkElement.Visibility" Value="Collapsed" /&gt;
                              &lt;/Style&gt;
                          &lt;/GridView.ColumnHeaderContainerStyle&gt;
                          &lt;GridView.Columns&gt;
                          &lt;GridViewColumn Header="Red" CellTemplate="{StaticResource RedItemTemplate}" /&gt;
                              &lt;GridViewColumn Header="Green" CellTemplate="{StaticResource GreenItemTemplate}" /&gt;
                              &lt;GridViewColumn Header="Black" CellTemplate="{StaticResource BlackItemTemplate}" /&gt;
                          &lt;/GridView.Columns&gt;
                      &lt;/GridView&gt;
                  &lt;/ListView.View&gt;
      &lt;/ListView&gt;
  </code></pre>
  
  <p><strong>Skin File</strong></p>
  
  <pre><code>    &lt;!--        Templates for Number ListBox            --&gt;
  &lt;DataTemplate x:Key="RedItemTemplate"&gt;
      &lt;Viewbox MaxHeight="140" VerticalAlignment="Stretch"&gt;
          &lt;TextBlock Text="{Binding Path=RedItem}" HorizontalAlignment="Center" Foreground="Red" /&gt;
      &lt;/Viewbox&gt;
  &lt;/DataTemplate&gt;
  
  &lt;DataTemplate x:Key="GreenItemTemplate"&gt;
      &lt;Viewbox MaxHeight="140" VerticalAlignment="Stretch"&gt;
          &lt;TextBlock Text="{Binding Path=GreenItem}" HorizontalAlignment="Center" Foreground="Green" /&gt;
      &lt;/Viewbox&gt;
  &lt;/DataTemplate&gt;
  
  &lt;DataTemplate x:Key="BlackItemTemplate"&gt;
      &lt;Viewbox MaxHeight="140" VerticalAlignment="Stretch"&gt;
          &lt;TextBlock Text="{Binding Path=BlackItem}" HorizontalAlignment="Center" Foreground="LightGray" /&gt;
      &lt;/Viewbox&gt;
  &lt;/DataTemplate&gt;
  
  &lt;Style x:Key="ListViewStyle" TargetType="{x:Type ListViewItem}"&gt;
      &lt;Setter Property="Background" Value="Transparent" /&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Stretch" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type ListViewItem}"&gt;
                  &lt;Border x:Name="Bd" Background="{TemplateBinding Background}"
                          SnapsToDevicePixels="True" BorderThickness="0,0,0,1" BorderBrush="Transparent"&gt;
                      &lt;GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /&gt;
                  &lt;/Border&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsSelected" Value="false" /&gt;
                      &lt;Trigger Property="IsMouseOver" Value="false" /&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p><strong>Custom Property</strong></p>
  
  <pre><code>    /// &lt;summary&gt;
  /// ListViewColumnStretch
  /// &lt;/summary&gt;
  public class ListViewColumns : DependencyObject
  {
  
      #region Fields
  
      /// &lt;summary&gt;
      /// Holds a reference to our parent ListView control
      /// &lt;/summary&gt;
      private ListView _parentListView = null;
  
      #endregion
  
      #region Dependency Property Infrastructure
  
      /// &lt;summary&gt;
      /// IsStretched Dependency property which can be attached to gridview columns.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty StretchProperty =
          DependencyProperty.RegisterAttached("Stretch",
          typeof(bool),
          typeof(ListViewColumns),
          new UIPropertyMetadata(true, null, OnCoerceStretch));
  
  
      /// &lt;summary&gt;
      /// Gets the stretch.
      /// &lt;/summary&gt;
      /// &lt;param name="obj"&gt;The obj.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static bool GetStretch(DependencyObject obj)
      {
          return (bool)obj.GetValue(StretchProperty);
      }
  
      /// &lt;summary&gt;
      /// Sets the stretch.
      /// &lt;/summary&gt;
      /// &lt;param name="obj"&gt;The obj.&lt;/param&gt;
      /// &lt;param name="value"&gt;if set to &lt;c&gt;true&lt;/c&gt; [value].&lt;/param&gt;
      public static void SetStretch(DependencyObject obj, bool value)
      {
          obj.SetValue(StretchProperty, value);
      }
  
      /// &lt;summary&gt;
      /// Called when [coerce stretch].
      /// &lt;/summary&gt;
      /// &lt;remarks&gt;If this callback seems unfamilar then please read
      /// the great blog post by Paul jackson found here. 
      /// http://compilewith.net/2007/08/wpf-dependency-properties.html&lt;/remarks&gt;
      /// &lt;param name="source"&gt;The source.&lt;/param&gt;
      /// &lt;param name="value"&gt;The value.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static object OnCoerceStretch(DependencyObject source, object value)
      {
          ListView lv = (source as ListView);
  
          //Ensure we dont have an invalid dependency object of type ListView.
          if (lv == null)
              throw new ArgumentException("This property may only be used on ListViews");
  
          //Setup our event handlers for this list view.
          lv.Loaded += new RoutedEventHandler(lv_Loaded);
          lv.SizeChanged += new SizeChangedEventHandler(lv_SizeChanged);
          return value;
      }
  
      #endregion
  
      #region Event Handlers
  
      /// &lt;summary&gt;
      /// Handles the SizeChanged event of the lv control.
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;The source of the event.&lt;/param&gt;
      /// &lt;param name="e"&gt;The &lt;see cref="System.Windows.SizeChangedEventArgs"/&gt; instance containing the event data.&lt;/param&gt;
      private static void lv_SizeChanged(object sender, SizeChangedEventArgs e)
      {
          ListView lv = (sender as ListView);
          if (lv.IsLoaded)
          {
              //Set our initial widths.
              SetColumnWidths(lv);
          }
      }
  
      /// &lt;summary&gt;
      /// Handles the Loaded event of the lv control.
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;The source of the event.&lt;/param&gt;
      /// &lt;param name="e"&gt;The &lt;see cref="System.Windows.RoutedEventArgs"/&gt; instance containing the event data.&lt;/param&gt;
      private static void lv_Loaded(object sender, RoutedEventArgs e)
      {
          ListView lv = (sender as ListView);
          //Set our initial widths.
          SetColumnWidths(lv);
      }
      #endregion
  
      #region Private Members
  
      /// &lt;summary&gt;
      /// Sets the column widths.
      /// &lt;/summary&gt;
      private static void SetColumnWidths(ListView listView)
      {
          //Pull the stretch columns fromt the tag property.
          List&lt;GridViewColumn&gt; columns = (listView.Tag as List&lt;GridViewColumn&gt;);
          double specifiedWidth = 0;
          GridView gridView = listView.View as GridView;
          if (gridView != null)
          {
              if (columns == null)
              {
                  //Instance if its our first run.
                  columns = new List&lt;GridViewColumn&gt;();
                  // Get all columns with no width having been set.
                  foreach (GridViewColumn column in gridView.Columns)
                  {
                      if (!(column.Width &gt;= 0))
                          columns.Add(column);
                      else specifiedWidth += column.ActualWidth;
                  }
              }
              else
              {
                  // Get all columns with no width having been set.
                  foreach (GridViewColumn column in gridView.Columns)
                      if (!columns.Contains(column))
                          specifiedWidth += column.ActualWidth;
              }
  
              // Allocate remaining space equally.
              foreach (GridViewColumn column in columns)
              {
                  double newWidth = (listView.ActualWidth - specifiedWidth) / columns.Count;
                  if (newWidth &gt;= 0) column.Width = newWidth - 10;
              }
  
              //Store the columns in the TAG property for later use. 
              listView.Tag = columns;
          }
      }
  
  
      #endregion
  
  }
  </code></pre>