我正在尝试将UniformGrid列属性绑定到ItemsControl中。
到目前为止,我已经:
<ScrollViewer x:Name="scroll" VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid>
<UniformGrid.Columns>
<MultiBinding Converter="{StaticResource Columns}">
<Binding RelativeSource="{RelativeSource Self}" />
<Binding Source="{x:Reference scroll}" />
</MultiBinding>
</UniformGrid.Columns>
</UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>在转换器中:
const double TileWidth = 154;
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
double width, aWidth;
UniformGrid grid = values[0] as UniformGrid;
ScrollViewer scroll = values[1] as ScrollViewer;
var gw = grid.Width;
var gaw = grid.ActualWidth;
aWidth = scroll.ActualWidth;
width = aWidth - (scroll.Padding.Left + scroll.Padding.Right);
return 3;
// return width / TileWidth;
}我无法获得父控件的任何宽度来确定要显示多少列。他们要么是0.0,要么是NaN。
如何获得父级的宽度来确定可用空间的大小?
发布于 2013-09-18 01:41:50
使用UniformGrid,尝试创建变量cols并绑定它。
<UniformGrid Columns="{Binding ElementName=_this, Path=TileColumns}"> 然后在后面的代码中,计算需要多少列,检查ActualWidth并设置该变量。
public int TileColumns
{
get { return (int)GetValue(TileColumnsProperty); }
set { SetValue(TileColumnsProperty, value); }
}
// Using a DependencyProperty as the backing store for TileColumns. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TileColumnsProperty =
DependencyProperty.Register("TileColumns", typeof(int), typeof(TileView), new PropertyMetadata(3));
private void scroll_SizeChanged(object sender, SizeChangedEventArgs e)
{
var aw = scroll.ActualWidth;
TileColumns = (int)aw / 154; // 154 is a Tile's width
}https://stackoverflow.com/questions/18862193
复制相似问题