2014-10-22 48 views
1

是否有任何可行的方法让行的大小可以调整为ItemsControl,ListView,ListBoxDataGridItemsControl,ListView,ListBox或DataGrid中可调整大小的行

我可以做的唯一方法我想是这样的:

<UniformGrid Columns="1"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <SomeControlGoesHere /> 
     <GridSplitter Height="5" Background="Transparent" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Grid.Row="1" /> 
    </Grid> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <SomeControlGoesHere /> 
     <GridSplitter Height="5" Background="Transparent" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Grid.Row="1" /> 
    </Grid> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <SomeControlGoesHere /> 
     <GridSplitter Height="5" Background="Transparent" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Grid.Row="1" /> 
    </Grid> 
</UniformGrid> 

这与3个部分,我可以调整的例子。我想让这个数字是任意的,并且绑定了数据,并且能够指定一个模板。

我不能使用任何ItemsControl重新创建此项,因为项目容器必须是ContentControl,而网格不是。如果网格包裹任何东西,它不会像我需要的那样工作。

是否有任何与这些控件的任何构建这样做?

是否有另一种方法来创建这样的可调整大小的部分?

更糟糕的是,有没有第三方控制这样做?

回答

0

因此,我发现最好的方法是为此创建正确的网格。

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <SomeControl /> 
    <GridSplitter Height="5" VerticalAlignment="Center" HorizontalAlignment="Stretch" Grid.Row="1" /> 
    <SomeControl Grid.Row="2" /> 
</Grid> 

所以我创建了一个继承Grid定制控件,这将让我的项目绑定到它。这不是全部的功能,几乎只有我需要的东西;一个是ItemTemplate

public class BindableGrid : Grid 
{ 
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(BindableGrid), new FrameworkPropertyMetadata(null, OnItemsSourceChanged)); 

    public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(BindableGrid), new FrameworkPropertyMetadata(null, OnItemTemplateChanged)); 

    public IEnumerable ItemsSource 
    { 
     get { return (IEnumerable)GetValue(ItemsSourceProperty); } 
     set { SetValue(ItemsSourceProperty, value); } 
    } 

    public DataTemplate ItemTemplate 
    { 
     get { return (DataTemplate)GetValue(ItemTemplateProperty); } 
     set { SetValue(ItemTemplateProperty, value); } 
    } 

    public static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     ((BindableGrid)d).OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue); 
    } 

    public void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) 
    { 
     INotifyCollectionChanged oldValueNotify; 
     if((oldValueNotify = oldValue as INotifyCollectionChanged) != null) 
     { 
      oldValueNotify.CollectionChanged -= ItemsSourceCollectionChanged; 
     } 

     INotifyCollectionChanged newValueNotify; 
     if((newValueNotify = newValue as INotifyCollectionChanged) != null) 
     { 
      newValueNotify.CollectionChanged += ItemsSourceCollectionChanged; 
     } 
    } 

    public static void OnItemTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     ((BindableGrid)d).OnItemTemplateChanged((DataTemplate)e.OldValue, (DataTemplate)e.NewValue); 
    } 

    public void OnItemTemplateChanged(DataTemplate oldItemTemplate, DataTemplate newItemTemplate) 
    { 
    } 

    private void ItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 
     if(e.Action == NotifyCollectionChangedAction.Reset) 
     { 
      RowDefinitions.Clear(); 
      Children.Clear(); 
     } 
     else if(e.Action == NotifyCollectionChangedAction.Add) 
     { 
      AddItems(e.NewItems); 
     } 
     else 
     { 
      throw new InvalidOperationException(string.Format("Action '{0}' is not valid.", e.Action)); 
     } 
    } 

    private void AddItems(IList items) 
    { 
     foreach(var item in items) 
     { 
      if(Children.Count > 0) 
      { 
       RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); 
       var gridSplitter = new GridSplitter 
       { 
        Height = 5, 
        Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)), 
        VerticalAlignment = VerticalAlignment.Center, 
        HorizontalAlignment = HorizontalAlignment.Stretch, 
       }; 
       SetRow(gridSplitter, Children.Count); 
       Children.Add(gridSplitter); 
      } 

      RowDefinitions.Add(new RowDefinition 
      { 
       Height = new GridLength(1, GridUnitType.Star), 
       MinHeight = 40, 
      }); 
      var contentPresenter = new ContentPresenter(); 
      contentPresenter.SetValue(ContentPresenter.ContentTemplateProperty, ItemTemplate); 
      contentPresenter.Content = item; 
      SetRow(contentPresenter, Children.Count); 
      Children.Add(contentPresenter); 
     } 
    } 
}