首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于父级宽度绑定UniformGrid列

基于父级宽度绑定UniformGrid列
EN

Stack Overflow用户
提问于 2013-09-18 00:53:51
回答 1查看 2.1K关注 0票数 2

我正在尝试将UniformGrid列属性绑定到ItemsControl中。

到目前为止,我已经:

代码语言:javascript
复制
<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>

在转换器中:

代码语言:javascript
复制
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

如何获得父级的宽度来确定可用空间的大小?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-18 01:41:50

使用UniformGrid,尝试创建变量cols并绑定它。

代码语言:javascript
复制
 <UniformGrid Columns="{Binding ElementName=_this, Path=TileColumns}"> 

然后在后面的代码中,计算需要多少列,检查ActualWidth并设置该变量。

代码语言:javascript
复制
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
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18862193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档