【发布时间】:2020-10-27 18:04:45
【问题描述】:
我有一个 ContentControl,我将其内容设置为 DataTemplate。我正在设置 ContentControl 的标记值。有没有办法在数据模板中访问此标记元素并将其作为命令参数传递。换句话说,我试图将标签作为参数传递给 DataTemplate。请帮忙。
<DataTemplate x:Key="SensorStatusControlTemplate" x:DataType="viewModel:SensorBufferState">
<Grid>
<Rectangle x:Name="SensorRectangle"
Fill="{x:Bind Converter={StaticResource SensorStateOverflowConverter},
ConverterParameter={What do I say here to get the Tag}}"
Height="30"
Width="125" />
<TextBlock x:Name="SensorTextBlock"
Text="{x:Bind Converter={StaticResource SensorStateOverflowConverter}}"
FontSize="{StaticResource FontSizeMedium}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="White" />
</Grid>
</DataTemplate>
这是我的 ControlTemplate。有没有办法访问 DataTemplate 中的 Tag?
<ContentControl Content="{Binding VmPRWControlData.OverflowSensorState,UpdateSourceTrigger=PropertyChanged}"
ContentTemplate="{StaticResource SensorStatusControlTemplate}"
Tag="Overflow"
HorizontalAlignment="Center"
Width="{Binding ElementName=LABLidSensorTextBlock,Path=ActualWidth}" />
编辑:我试过这样做,但参数值为空,
ConverterParameter={Binding Tag, RelativeSource={RelativeSource Mode=TemplatedParent}}
【问题讨论】:
-
你试过
TemplateBinding或{Binding RelativeSource=TemplateParent}吗? -
不让我试试。
-
TemplatedParent 不起作用。
标签: wpf datatemplate