【问题标题】:Accesing the Tag Element from Content Control and access it inside the Data Template从内容控件访问标签元素并在数据模板中访问它
【发布时间】: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


【解决方案1】:

你应该使用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}" />

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2017-03-26
  • 1970-01-01
  • 2021-10-27
  • 2017-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多