gpt4 book ai didi

c# - 根据父宽度绑定(bind) UniformGrid 列

转载 作者:行者123 更新时间:2023-11-30 22:13:48 24 4
gpt4 key购买 nike

我正在尝试在 ItemsControl 中绑定(bind) UniformGrid Columns 属性。

到目前为止我有:

<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.0NaN

如何获取父项的宽度以确定有多少可用空间?

最佳答案

使用 UniformGrid,尝试创建一个变量 cols 并绑定(bind)它

 <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
}

关于c# - 根据父宽度绑定(bind) UniformGrid 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18862193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com