WPF中使用ItemsControl嵌套绑定,在ItemsControl中嵌套一个ItemsControl,然后使用绑定(2)
<div id="cnblogs_post_body">为了能够适应不同分辨率的显示器,所以把第一层 DataTemplate 的 Width 属性和 SystemParameters.PrimaryScreenWidth 绑定了,实际上是通过一个 Converter 实现的(见 Source)。第二层 ItemsControl 的 DataTemplate 的 width 属性也想绑定到 SystemParameters.PrimaryScreenWidth,于是也自定义了一个 Converter。并且通过<local:MyConverter2 x:Key="C2ItemW"/> 引入。
需要说明的是,如果两层 DataTemplate 声明称一个 Resource,这时第二层 DataTemplate 的绑定总是不起作用(可能是我方法不对)。如果把第二层的提出来,单独作为一个 Resource声明。然后在第一层 DataTemplate 中通过 ItemTemplate指定就可以了。
下面上Source:
主ItemsControl:
<div class="cnblogs_code"><ItemsControl Background="Green" ItemTemplate="{StaticResource xItemTemplate}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" VerticalAlignment="Center"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl>
页:
[1]