2011-03-12 70 views
0

我正在使用擴展自定義屬性ListViewcolumns.Stretch的ListView框。這樣我就可以有3列,無論窗口大小如何均勻分佈。我的問題是在列表視圖項目被「切斷」的末尾如何防止ListView切斷項目

所以我的列表視圖包含3列,紅色,插入空白文本。它將項目插入0位置,因此它始終顯示在頂部並將其他所有內容向下推。滾動條默認是關閉的。

- 目標是讓3列佔據整個窗口空間,而不管窗口有多大或多小,並適當調整文本大小(最大高度約爲100-150)。然後,當行填滿整個集合時,最下面一行不能「跨越」視圖,它應該全部在視圖中或全部視圖中。

反正 - 代碼。任何幫助,將不勝感激,我可以硬編碼視框的maxheight,直到我發現,我的目標顯示器上工作,但最好我希望它來調整適當

的ListView

<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"> 
       <ListView.View> 
        <GridView> 
         <GridView.ColumnHeaderContainerStyle> 
          <Style> 
           <Setter Property="FrameworkElement.Visibility" Value="Collapsed" /> 
          </Style> 
         </GridView.ColumnHeaderContainerStyle> 
         <GridView.Columns> 
         <GridViewColumn Header="Red" CellTemplate="{StaticResource RedItemTemplate}" /> 
          <GridViewColumn Header="Green" CellTemplate="{StaticResource GreenItemTemplate}" /> 
          <GridViewColumn Header="Black" CellTemplate="{StaticResource BlackItemTemplate}" /> 
         </GridView.Columns> 
        </GridView> 
       </ListView.View> 
    </ListView> 

完美的大小皮膚文件

<!--  Templates for Number ListBox   --> 
<DataTemplate x:Key="RedItemTemplate"> 
    <Viewbox MaxHeight="140" VerticalAlignment="Stretch"> 
     <TextBlock Text="{Binding Path=RedItem}" HorizontalAlignment="Center" Foreground="Red" /> 
    </Viewbox> 
</DataTemplate> 

<DataTemplate x:Key="GreenItemTemplate"> 
    <Viewbox MaxHeight="140" VerticalAlignment="Stretch"> 
     <TextBlock Text="{Binding Path=GreenItem}" HorizontalAlignment="Center" Foreground="Green" /> 
    </Viewbox> 
</DataTemplate> 

<DataTemplate x:Key="BlackItemTemplate"> 
    <Viewbox MaxHeight="140" VerticalAlignment="Stretch"> 
     <TextBlock Text="{Binding Path=BlackItem}" HorizontalAlignment="Center" Foreground="LightGray" /> 
    </Viewbox> 
</DataTemplate> 

<Style x:Key="ListViewStyle" TargetType="{x:Type ListViewItem}"> 
    <Setter Property="Background" Value="Transparent" /> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListViewItem}"> 
       <Border x:Name="Bd" Background="{TemplateBinding Background}" 
         SnapsToDevicePixels="True" BorderThickness="0,0,0,1" BorderBrush="Transparent"> 
        <GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="false" /> 
        <Trigger Property="IsMouseOver" Value="false" /> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

自定義屬性

/// <summary> 
/// ListViewColumnStretch 
/// </summary> 
public class ListViewColumns : DependencyObject 
{ 

    #region Fields 

    /// <summary> 
    /// Holds a reference to our parent ListView control 
    /// </summary> 
    private ListView _parentListView = null; 

    #endregion 

    #region Dependency Property Infrastructure 

    /// <summary> 
    /// IsStretched Dependency property which can be attached to gridview columns. 
    /// </summary> 
    public static readonly DependencyProperty StretchProperty = 
     DependencyProperty.RegisterAttached("Stretch", 
     typeof(bool), 
     typeof(ListViewColumns), 
     new UIPropertyMetadata(true, null, OnCoerceStretch)); 


    /// <summary> 
    /// Gets the stretch. 
    /// </summary> 
    /// <param name="obj">The obj.</param> 
    /// <returns></returns> 
    public static bool GetStretch(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(StretchProperty); 
    } 

    /// <summary> 
    /// Sets the stretch. 
    /// </summary> 
    /// <param name="obj">The obj.</param> 
    /// <param name="value">if set to <c>true</c> [value].</param> 
    public static void SetStretch(DependencyObject obj, bool value) 
    { 
     obj.SetValue(StretchProperty, value); 
    } 

    /// <summary> 
    /// Called when [coerce stretch]. 
    /// </summary> 
    /// <remarks>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</remarks> 
    /// <param name="source">The source.</param> 
    /// <param name="value">The value.</param> 
    /// <returns></returns> 
    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 

    /// <summary> 
    /// Handles the SizeChanged event of the lv control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.Windows.SizeChangedEventArgs"/> instance containing the event data.</param> 
    private static void lv_SizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     ListView lv = (sender as ListView); 
     if (lv.IsLoaded) 
     { 
      //Set our initial widths. 
      SetColumnWidths(lv); 
     } 
    } 

    /// <summary> 
    /// Handles the Loaded event of the lv control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> 
    private static void lv_Loaded(object sender, RoutedEventArgs e) 
    { 
     ListView lv = (sender as ListView); 
     //Set our initial widths. 
     SetColumnWidths(lv); 
    } 
    #endregion 

    #region Private Members 

    /// <summary> 
    /// Sets the column widths. 
    /// </summary> 
    private static void SetColumnWidths(ListView listView) 
    { 
     //Pull the stretch columns fromt the tag property. 
     List<GridViewColumn> columns = (listView.Tag as List<GridViewColumn>); 
     double specifiedWidth = 0; 
     GridView gridView = listView.View as GridView; 
     if (gridView != null) 
     { 
      if (columns == null) 
      { 
       //Instance if its our first run. 
       columns = new List<GridViewColumn>(); 
       // Get all columns with no width having been set. 
       foreach (GridViewColumn column in gridView.Columns) 
       { 
        if (!(column.Width >= 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 >= 0) column.Width = newWidth - 10; 
      } 

      //Store the columns in the TAG property for later use. 
      listView.Tag = columns; 
     } 
    } 


    #endregion 

} 
+1

哪一端被切斷?右側還是底部? – coldandtired

+0

底部正在切斷,柱拉伸的目的是防止柱被切斷 –

回答

0

這有點間接,但是當面對一個控件的限制時,爲什麼不嘗試不同的方法?而不是使用ListView的GridView,嘗試使用DataGrid,它允許按比例增長列。我只想從你的」 XAML發佈精簡版,但你會得到點(這是WIDTH =‘33 *’)

<DataGrid> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Red" Width="33*"/> 
     <DataGridTextColumn Header="Green" Width="33*"/> 
     <DataGridTextColumn Header="Black" Width="33*"/> 
    </DataGrid.Columns> 
</DataGrid> 
2

我知道這是一個老問題,但在這裏是另一個解決方案爲其他人。在ListViewItem的樣式上,還將Horizo​​ntalAlignment設置爲Stretch和With設置爲Auto。

<!--ListViewItem--> 
    <Style TargetType="ListViewItem" x:Key="ListViewItemStyle"> 
     <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
     <Setter Property="HorizontalAlignment" Value="Stretch" /> 
     <Setter Property="Width" Value="Auto" /> 
    </Style>