我有一个小问题如何设置ItemsControl
Property Width
。我Datatemplates
用来填充我的ItemControl
. 每个项目Grid
都是Canvas
.
当我ItemsControl
在 aWindow
中使用ActualWidth
(and DesiredWidth
) 时,总是0.0
.
只有当我对 ItemsControl的Width
属性进行硬编码时,它才有大小。Canvas
但这远非完美。我宁愿从最大的项目中Control
获取width
(或)。DesiredWidth
这是一个简短的代码示例:
<ItemsControl ItemsSource="{Binding mySource}"
ItemTemplate="{DynamicResource myTemplate}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type type:myType}" x:Key="myTemplate">
<Grid Width="Auto" Height="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="1"
Height="1"
HorizontalAlignment="Stretch"
Fill="Blue"/>
<TextBlock Grid.Row="0"
Grid.Column="1"
Margin="3"
Text="{Binding Text}"
FontWeight="Bold"
TextWrapping="Wrap"
TextAlignment="Left"/>
</Grid>
</DataTemplate>
<v:PositionMultiConverter x:Key="PositionMultiConverter"/>
</ItemsControl.Resources>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Bottom">
<Setter.Value>
<MultiBinding Converter="{StaticResource ResourceKey=PositionMultiConverter}">
<Binding Path="Position"/>
<Binding Path="ActualHeight" ElementName="canvas"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas x:Name="canvas" IsItemsHost="True" Width="20"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>