我希望将具有ListView的视图模型项(从源中)对齐为ItemsPanel,这样项目基本上是在两列中对齐的,但是如果项大于一列,则将其包装,从而使其行更高。
我希望通过具有与具有两个列的Grid控件相同宽度的ListView控件来实现这一点,然后从Grid的一个ColumnDefinition的ActualWidth获取(绑定)一个项的宽度。
因此,我创建了以下XAML:
<Grid>
<Grid.ColumnDefinitions>
<!--The column to bind from is a bit less wide than 50% to enable some margin between items in the same row.-->
<ColumnDefinition x:Name="clm1" Width="45*"/>
<ColumnDefinition Width="55*"/>
</Grid.ColumnDefinitions>
<ListView Grid.ColumnSpan="2" Width="some width" Height="some height" ItemsSource="some source">
<!--the wrap panel that wraps items after two is in a row-->
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel ItemWidth="{Binding ActualWidth, ElementName=clm1}" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<!--some properties are binded to the view model object in the DataContext-->
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}">
<!--separate TextBlock is for text wrapping-->
<TextBlock Text="{Binding Text}" TextWrapping="Wrap"/>
</CheckBox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>但是,这不起作用,项目是按照常规WrapPanel而不是列对齐的。
原因为什么不能工作:我(在Live中)检查了绑定到ActualWidth的值是什么,这是double.NaN,尽管ActualWidth属性有一个正常的数值。
因此,主要的问题是:为什么绑定不能工作?(第二个问题是:如何正确实现所需的布局?)
发布于 2017-08-17 19:24:59
创建视图模型取决于所需的解决方案体系结构。例如,我将MSDN MVVM模式用于较大的项目,因为它允许更好的单元测试。在我看来,当您需要进行复杂的绑定并处理大量数据时,视图模型是一个更优雅的解决方案。
https://stackoverflow.com/questions/45741611
复制相似问题