【发布时间】:2021-10-30 19:00:11
【问题描述】:
我有一个包含字符串消息和状态枚举的数据对象。 我的目标是根据状态值设置文本块的颜色。
数据模板:
<DataTemplate DataType="{x:Type interfaces_general1:AnalogPinData}" x:Key="analogSignalTestTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Width="135" Text="{Binding Path=ThresholdDescriptor.Message}" Style="{StaticResource colorfulTextBlockStyleAnalogPin}" />
</StackPanel>
</DataTemplate>
风格
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource emphasizableTextBlock}" x:Key="colorfulTextBlockStyleAnalogPin">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource /*need to get here to a data object property*/}, Path=??}" Value="Info">
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource= /*need to get here to a data object property*/, Path=Text}" Value="Warning">
<Setter Property="Foreground" Value="Orange" />
</DataTrigger>
</Style.Triggers>
</Style>
我怎样才能绑定到状态属性
数据对象是:
public class AnalogPinData
{
/// <summary>
/// Pin information
/// </summary>
[PublicAPI]
public AnalogPin Pin { get; set; }
/// <summary>
/// Threshold definition information
/// </summary>
[PublicAPI]
public ThresholdDescriptor ThresholdDescriptor { get; set; }
}
public class ThresholdDescriptor
{
public Range Range { get; set; }
[PublicAPI]
public StatusIndicator Status { get; set; }
[PublicAPI]
public string Message { get; set; }
}
我需要在绑定中设置 Message 和 Status 道具。
谢谢!
【问题讨论】:
标签: c# wpf xaml binding datatemplate