我有一个ItemsControl,其DataTemplate绑定到一个整数类型的ObservableCollection。
在Windows资源中:
<ItemsControl Name="DimsContainer" ItemTemplate="{StaticResource DimensionsTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
在Windows资源中:
<Window.Resources>
<DataTemplate x:Key="DimensionsTemplate" >
<TextBlock Text="{Binding}"
Padding="5"
VerticalAlignment="Center"
FontSize="32"/>
</DataTemplate>
</Window.Resources>
我的问题是,在代码中,我需要能够确定ItemsControl中TextBlocks(或者如果以后更改了元素,就是任何元素)的宽度。 有人有想法如何做到这一点吗?
当我使用DimsContainer.Items[i]时,它给出绑定的项而不是TextBlock。