使用转换器的数据触发不起作用

发布于 2024-12-08 23:16:09 字数 1751 浏览 0 评论 0原文

我正在尝试根据文本块的值更新其颜色。看似简单却行不通。

这是文本块 xaml。

 <TextBlock 
        Grid.Column="1" 
        Grid.Row="1" 
        Text="{Binding Path=GL, StringFormat={}{0:N0}}" 
        HorizontalAlignment="Left" 
        FontFamily="Verdana" 
        Foreground="Tomato" 
        FontWeight="Bold"             
        VerticalAlignment="Center"
        Margin="5,2,5,0"
        FontSize="18"
        >
        <TextBlock.Resources>
            <converters:ColorConverter x:Key="CoConverter"></converters:ColorConverter>
        </TextBlock.Resources>
      <TextBlock.Style>
          <Style>
              <Style.Triggers>
                  <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=CoConverter}}" Value="true">
                  <Setter Property="TextBlock.Foreground" Value="LimeGreen" />
                      </DataTrigger>
              </Style.Triggers>
          </Style>
      </TextBlock.Style>

这里的转换器

public class ColorConverter : MarkupExtension, IValueConverter
{

    #region IValueConverter Members

    public object Convert(object value, 
        Type targetType, 
        object parameter, 
        System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return false;
        if (value.ToString().Length == 0)
            return false;
        if (System.Convert.ToDouble(value) >= 0)
            return true;

        return false;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

转换器看起来不错,但是由于某种原因触发器没有应用。

I am trying to update the color of textblock depending on it value. Seems simple however not working.

Here's the textblock xaml.

 <TextBlock 
        Grid.Column="1" 
        Grid.Row="1" 
        Text="{Binding Path=GL, StringFormat={}{0:N0}}" 
        HorizontalAlignment="Left" 
        FontFamily="Verdana" 
        Foreground="Tomato" 
        FontWeight="Bold"             
        VerticalAlignment="Center"
        Margin="5,2,5,0"
        FontSize="18"
        >
        <TextBlock.Resources>
            <converters:ColorConverter x:Key="CoConverter"></converters:ColorConverter>
        </TextBlock.Resources>
      <TextBlock.Style>
          <Style>
              <Style.Triggers>
                  <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=CoConverter}}" Value="true">
                  <Setter Property="TextBlock.Foreground" Value="LimeGreen" />
                      </DataTrigger>
              </Style.Triggers>
          </Style>
      </TextBlock.Style>

Here the converter

public class ColorConverter : MarkupExtension, IValueConverter
{

    #region IValueConverter Members

    public object Convert(object value, 
        Type targetType, 
        object parameter, 
        System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return false;
        if (value.ToString().Length == 0)
            return false;
        if (System.Convert.ToDouble(value) >= 0)
            return true;

        return false;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

The converter looks good, however the trigger is not applying for some reason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初心 2024-12-15 23:16:09
<TextBlock 
        Grid.Column="1" 
        Grid.Row="1" 
        Text="{Binding Path=GL, StringFormat={}{0:N0}}" 
        HorizontalAlignment="Left" 
        FontFamily="Verdana"
        FontWeight="Bold"             
        VerticalAlignment="Center"
        Margin="5,2,5,0"
        FontSize="18"
        >
        <TextBlock.Resources>
            <converters:ColorConverter x:Key="CoConverter"></converters:ColorConverter>
        </TextBlock.Resources>
      <TextBlock.Style>
          <Style>
              <Setter Property="TextBlock.Foreground" Value="Tomato" />
              <Style.Triggers>
                  <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=CoConverter}}" Value="true">
                  <Setter Property="TextBlock.Foreground" Value="LimeGreen" />
                      </DataTrigger>
              </Style.Triggers>
          </Style>
      </TextBlock.Style>

您需要在样式中设置 Foreground 属性,以便在运行时动态更改它。

<TextBlock 
        Grid.Column="1" 
        Grid.Row="1" 
        Text="{Binding Path=GL, StringFormat={}{0:N0}}" 
        HorizontalAlignment="Left" 
        FontFamily="Verdana"
        FontWeight="Bold"             
        VerticalAlignment="Center"
        Margin="5,2,5,0"
        FontSize="18"
        >
        <TextBlock.Resources>
            <converters:ColorConverter x:Key="CoConverter"></converters:ColorConverter>
        </TextBlock.Resources>
      <TextBlock.Style>
          <Style>
              <Setter Property="TextBlock.Foreground" Value="Tomato" />
              <Style.Triggers>
                  <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=CoConverter}}" Value="true">
                  <Setter Property="TextBlock.Foreground" Value="LimeGreen" />
                      </DataTrigger>
              </Style.Triggers>
          </Style>
      </TextBlock.Style>

You need to set the Foreground property in your style to change it dynamically at runtime.

爱冒险 2024-12-15 23:16:09

指定转换器时,您似乎在大括号内缺少 StaticResource:

Converter={StaticResource converters:ColorConverter}

Looks like you're missing StaticResource inside your curly braces when specifying the converter:

Converter={StaticResource converters:ColorConverter}
じее 2024-12-15 23:16:09

如果您尝试动态更改属性值,则相应的属性必须仅保留在 Setter 标记中。

<TextBlock>
  <TextBlock.Style>
      <Style>
          <Setter Property="TextBlock.Foreground" Value="Tomato" />
          <Style.Triggers>
              <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=CoConverter}}" Value="true">
              <Setter Property="TextBlock.Foreground" Value="LimeGreen" />
                  </DataTrigger>
          </Style.Triggers>
      </Style>
</TextBlock.Style>

但不能同时出现在 TextBlock 和 Setter 标记中。为了准确地说明您的示例,请删除我的代码中给出的 TextBlock 标记内的 Foreground 属性。

If you are trying to change property value dynamically, then corresponding property must be kept only in Setter tag.

<TextBlock>
  <TextBlock.Style>
      <Style>
          <Setter Property="TextBlock.Foreground" Value="Tomato" />
          <Style.Triggers>
              <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=CoConverter}}" Value="true">
              <Setter Property="TextBlock.Foreground" Value="LimeGreen" />
                  </DataTrigger>
          </Style.Triggers>
      </Style>
</TextBlock.Style>

But not in both TextBlock and Setter tag. To be precise for your example remove Foreground property inside the TextBlock tag as given in my code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文