【发布时间】:2014-06-12 13:31:56
【问题描述】:
我正在根据画布的位置在画布中安排几个文本块。示例代码 sn-p 在底部提供。对于这个过程,我需要访问 ItemContainerStyle 中每个文本块的实际宽度和实际高度。我正在努力按元素名称访问它的属性,如下所示,因为它没有名称。怎么做?我需要通过模板吗?
<MultiBinding Converter="{StaticResource BPositionConverter}" ConverterParameter="Left">
<Binding ElementName="TextBlock" Path="ActualHeight" /> <---Problem
<Binding ElementName="TextBlock" Path="ActualWidth" /> <---Problem
</MultiBinding>
--完整的sn-p
<ItemsControl ItemsSource="{Binding Locations}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="Point">
<TextBlock Text="{Binding}" Width="40" Height="20" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left">
<Setter.Value>
<MultiBinding Converter="{StaticResource BPositionConverter}" ConverterParameter="Left">
<Binding ElementName="TextBlock" Path="ActualHeight" /> <---Problem
<Binding ElementName="TextBlock" Path="ActualWidth" /> <---Problem
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
【问题讨论】:
-
您能告诉我们您要达到的目标吗?似乎您想操作 Canvas.Left,所以想知道 BPositionConverter 对 ActualHeight 和 ActualWidth 做了什么。可能有另一种方法而不指定文本块的名称
-
@pushpraj,请检查这个问题。它说所有stackoverflow.com/questions/23000804/…实际上是用于根据参考点在画布中设置textpox位置,我仍然需要向多绑定转换器添加更多参数
标签: wpf wpf-controls