Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何基于列表中的值有条件地呈现文本?_C#_Wpf_Xaml - Fatal编程技术网

C# 如何基于列表中的值有条件地呈现文本?

C# 如何基于列表中的值有条件地呈现文本?,c#,wpf,xaml,C#,Wpf,Xaml,我正在使用列表框在我的C#用户控件中显示一个列表。这是我的清单: private ObservableCollection<AlarmInfo> _widgetData = new ObservableCollection<AlarmInfo>() { new AlarmInfo { AlarmType = "warning", AlarmLabel = "PLC Alarm 1",

我正在使用
列表框
在我的C#用户控件中显示一个列表。这是我的清单:

private ObservableCollection<AlarmInfo> _widgetData = new ObservableCollection<AlarmInfo>() {
    new AlarmInfo
    {
        AlarmType = "warning",
        AlarmLabel = "PLC Alarm 1",
        AlarmTimeStamp = new DateTime()
    },
    new AlarmInfo
    {
        AlarmType = "error",
        AlarmLabel = "Vision Error A",
        AlarmTimeStamp = new DateTime()
    },
    new AlarmInfo
    {
        AlarmType = "warning",
        AlarmLabel = "PLC Alarm 2",
        AlarmTimeStamp = new DateTime()
    },
    new AlarmInfo
    {
        AlarmType = "warning",
        AlarmLabel = "PLC Alarm 3",
        AlarmTimeStamp = new DateTime()
    },
};

我对这一技术很陌生。有人能帮我吗?

您可以使用
DataTriggers
检查值并相应地设置
文本

<TextBlock DataContext="{Binding AlarmType}">
   <TextBlock.Style>
      <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
         <Setter Property="Text" Value="{x:Static system:String.Empty}"/>
         <Style.Triggers>
            <DataTrigger Binding="{Binding}" Value="warning">
               <Setter Property="Text" Value="W"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding}" Value="error">
               <Setter Property="Text" Value="E"/>
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </TextBlock.Style>
</TextBlock>
在XAML中,您可以创建转换器的实例,并将其用于
文本
绑定

<DataTemplate>
   <DataTemplate.Resources>
      <local:AlarmTypeConverter x:Key="AlarmTypeConverter"/>
   </DataTemplate.Resources>
   <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding AlarmType, Converter={StaticResource AlarmTypeConverter}}"/>
   </StackPanel>
</DataTemplate>

使用绑定转换器或数据触发器。在这两种情况下,AlarmType最好是一个枚举。@克莱门斯,可以用字符串来完成吗?非常感谢。这正如预期的那样有效。
public class AlarmTypeConverter : IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {
      if (!(value is string alarmType))
         return null;

      switch (alarmType)
      {
         case "warning": return "W";
         case "error": return "E";
         default: return null;
      }
   }

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
   {
      throw new NotImplementedException("Not needed right now.");
   }
}
<DataTemplate>
   <DataTemplate.Resources>
      <local:AlarmTypeConverter x:Key="AlarmTypeConverter"/>
   </DataTemplate.Resources>
   <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding AlarmType, Converter={StaticResource AlarmTypeConverter}}"/>
   </StackPanel>
</DataTemplate>
public enum AlarmType
{
   Warning,
   Error
}