我想水平放置ItemControl中的項目,同時使他們填充父控件。WPF ItemsControl水平方向和填充父項?
這是我的XAML:
<ItemsControl ItemsSource="{Binding AnnualWeatherViewModels}" Visibility="{Binding IsAnnualWeatherViewModels, Converter={StaticResource VisibilityConverter}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<V:AerosolSimpleWeatherCharacteristicsView DataContext="{Binding}"></V:AerosolSimpleWeatherCharacteristicsView>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
兩個變化我已經嘗試了ItemsControl.ItemsPanel
是:
<StackPanel Orientation="Horizontal"></StackPanel>
和:
<DockPanel LastChildFill="False"></DockPanel>
但是沒有達到預期的結果, StackPanel
壓縮所有項目和DockPanel
將e ither用專用於最後一項的大部分空間填充父控件的空間,或根據LastChildFill
的值填充父空間。
那麼我該如何佈局我的ItemsControl
的項目水平,並讓他們填寫家長控制的空間?
我結束了創建爲sugested在回答這個自定義控制:
public partial class StretchingPanel : Grid
{
public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register("Orientation", typeof(Orientation), typeof(StretchingPanel), new UIPropertyMetadata(System.Windows.Controls.Orientation.Horizontal));
public static readonly DependencyProperty FillFirstItemProperty =
DependencyProperty.Register("FillFirstItem", typeof(bool), typeof(StretchingPanel), new UIPropertyMetadata(true));
public static readonly DependencyProperty FillFirstItemFactorProperty =
DependencyProperty.Register("FillFirstItemFactor", typeof(double), typeof(StretchingPanel), new UIPropertyMetadata(1.8));
public Orientation Orientation
{
get
{
return (Orientation)GetValue(OrientationProperty);
}
set
{
SetValue(OrientationProperty, value);
}
}
public bool FillFirstItem
{
get
{
return (bool)GetValue(FillFirstItemProperty);
}
set
{
SetValue(FillFirstItemProperty, value);
}
}
public double FillFirstItemFactor
{
get
{
return (double)GetValue(FillFirstItemFactorProperty);
}
set
{
SetValue(FillFirstItemFactorProperty, value);
}
}
protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
{
if (Orientation == System.Windows.Controls.Orientation.Horizontal)
{
ColumnDefinitions.Clear();
for (int i = 0; i < Children.Count; ++i)
{
var column = new ColumnDefinition();
if (i == 0 && FillFirstItem)
column.Width = new GridLength(FillFirstItemFactor, GridUnitType.Star);
ColumnDefinitions.Add(column);
Grid.SetColumn(Children[i], i);
}
}
else
{
RowDefinitions.Clear();
for (int i = 0; i < Children.Count; ++i)
{
var row = new RowDefinition();
if (i == 0 && FillFirstItem)
row.Height = new GridLength(FillFirstItemFactor, GridUnitType.Star);
RowDefinitions.Add(row);
Grid.SetRow(Children[i], i);
}
}
base.OnVisualChildrenChanged(visualAdded, visualRemoved);
}
public StretchingPanel()
{
InitializeComponent();
}
}
屬性'的DataContext = 「{結合}」'是多餘的。去掉它。這與以下C#代碼有些相同:'this.DataContext = this.DataContext;' – 2013-05-12 22:36:35
改用'/>'結束XAML元素也很方便。對於實例:' V:AerosolSimpleWeatherCharacteristicsView>'可以簡化爲' ' –
2013-05-12 22:39:14
感謝提示。 – 2013-05-12 22:45:32