从内容控件访问标签元素并在数据模板中访问它

问题描述

我有一个ContentControl,将其内容设置为DataTemplate。我正在设置ContentControl的Tag值。有没有一种方法可以访问数据模板中的此Tag元素并将其作为CommandParameter传递。换句话说,我试图将Tag作为参数传递给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中的标签

<ContentControl Content="{Binding VmPRWControlData.OverflowSensorState,UpdateSourceTrigger=PropertyChanged}"
                                        ContentTemplate="{StaticResource SensorStatusControlTemplate}"
                                        Tag="Overflow"
                                        HorizontalAlignment="Center"
                                        Width="{Binding ElementName=LABLidSensorTextBlock,Path=ActualWidth}" />

编辑:我已经尝试过这样做,但是参数值为null,

ConverterParameter={Binding Tag,RelativeSource={RelativeSource Mode=TemplatedParent}}

解决方法

您应使用RelativeSource.AncestorType遍历树以找到父控件:

<DataTemplate DataType="{x:Type viewModel:SensorBufferState}">
  <Button CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContentControl},Path=Tag}"/>
</DataTemplate>

正如您正确提到的那样,UWP不支持RelativeSource.AncestorType

以下解决方案也适用于WPF:

解决方案1 ​​

您可以改用Binding.ElementName

App.xaml

<DataTemplate x:Key="DataTemplate">
  <Button CommandParameter="{Binding ElementName=ContentControl,Path=Tag}"/>
</DataTemplate>

MainPage.xaml

<ContentControl x:Name="ContentControl" 
                Tag="123"  
                ContentTemplate="{StaticResource DataTemplate}" />

解决方案2

或者使用DataContext设置为视图模型或DependencyProperty代替Tag属性:

App.xaml

<DataTemplate x:Key="DataTemplate">
  <Button CommandParameter="{Binding CommandParameterValue}"/>
</DataTemplate>

MainPage.xaml.cs

public sealed partial class MainPage : Page
{
  public static readonly DependencyProperty CommandParameterValueProperty = DependencyProperty.Register(
    "CommandParameterValue",typeof(string),typeof(MainPage),new PropertyMetadata(default(string)));

  public string CommandParameterValue 
  { 
    get => (string) GetValue(MainPage.CommandParameterValueProperty); 
    set => SetValue(MainPage.CommandParameterValueProperty,value); 
  }

  public MainPage()
  {
    this.InitializeComponent();
    this.DataContext = this;
    this.CommandParameterValue = "ABC";
  }
}

MainPage.xaml

<ContentControl ContentTemplate="{StaticResource DataTemplate}" />