从内容控件访问标签元素并在数据模板中访问它 [英] Accesing the Tag Element from Content Control and access it inside the Data Template

查看:53
本文介绍了从内容控件访问标签元素并在数据模板中访问它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ContentControl,将其内容设置为DataTemplate.我正在设置ContentControl的Tag值.有没有一种方法可以访问数据模板中的此Tag元素并将其作为CommandParameter传递.换句话说,我正在尝试将Tag作为参数传递给DataTemplate.请帮忙.

I have a ContentControl where I am setting its content to a DataTemplate. I am setting the Tag value of the ContentControl. Is there a way to access this Tag Element in the Data Template and pass it as CommandParameter. In other words I am trying to pass the Tag as a parameter to the DataTemplate. Please help.

   <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中的标签?

Here is my ControlTemplate. Is there a way to access the Tag in the DataTemplate?

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

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

I have tried doing like this but the parameter value is null,

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

推荐答案

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

You should traverse the tree to find the parent control using RelativeSource.AncestorType:

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


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

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

The following solutions work with WPF too:

您可以改用 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 属性:


Solution 2

Or alternatively use the DataContext set to a view model or a DependencyProperty instead of the Tag property:

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}" />

这篇关于从内容控件访问标签元素并在数据模板中访问它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆