值转换器不应用所需的字符串格式

Value Converter does not apply the desired string format

我一直在尝试实现一个简单的值转换器,但我不明白为什么未应用 StringFormat。正值变为绿色,负值变为红色,但我没有得到想要的结果 (+15.74),而是得到:15.74747474。

我怎样才能让它工作?有人可以告诉我需要更改的内容吗?

XAML

<Window.Resources>

    <local:PositiveConverter x:Key="PositiveConverter"/>

    <Style TargetType="TextBlock" x:Key="NumericTextBlock">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource PositiveConverter}}" Value="True">
                <Setter Property="Foreground" Value="Green"/>
                <Setter Property="Text" Value="{Binding StringFormat='({0:+0.0})'}"/>
            </DataTrigger>

            <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource PositiveConverter}}" Value="False">
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="Text" Value="{Binding StringFormat='({0:-0.0})'}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

</Window.Resources>

    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding ActualValue}" Style="{StaticResource NumericTextBlock}"/>
        </StackPanel>   
    </Grid>

ViewModel

namespace My.ViewModel
{
public class LocationViewModel : UserControl, INotifyPropertyChanged
{
public MyViewModel()
{
        SetActualValue();
}

public DateTime mDate = DateTime.Now;
public DateTime MDate
{  
get { return mDate; }
set
    {
        if (value == mDate)
        {
            return;
        }
        else
        {
            mDate = value;
            OnPropertyChanged("MDate");
            SetActualValue();
        }

    }

}

double _actualValue;
public double ActualValue
{
    get { return _actualValue; }
    set
    {
    _actualValue = value;
    OnPropertyChanged("ActualValue");
    }
}

值转换器

public class PositiveConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var number = 0.0;
        var isNumber = double.TryParse(value.ToString(), out number);

        return isNumber && number >= 0.0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

设置

Text="{Binding ActualValue}"

直接在 TextBlock 上 value precedence 比样式 Setter 更高

<Setter Property="Text" Value="{Binding StringFormat='({0:+0.0})'}"/>

因此 Setter 中的绑定将永远不会被应用。它们也写错了,因为它们错过了 属性 路径 ActualValue

你的方法行不通。您应该使用带有绑定到视图模型 属性 的 DataTriggers,如下所示:

<Style TargetType="TextBlock" x:Key="NumericTextBlock">
    <Setter Property="Text"
            Value="{Binding ActualValue, StringFormat='({0:+0.0})'}"/>

    <Style.Triggers>
        <DataTrigger Binding="{Binding ActualValue, Converter={StaticResource PositiveConverter}}"
                     Value="True">
            <Setter Property="Foreground" Value="Green"/>
            <Setter Property="Text"
                    Value="{Binding ActualValue, StringFormat='({0:+0.0})'}"/>
        </DataTrigger>

        <DataTrigger Binding="{Binding ActualValue, Converter={StaticResource PositiveConverter}}"
                     Value="False">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="Text"
                    Value="{Binding ActualValue, StringFormat='({0:-0.0})'}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

并从 TextBlock 中删除 Text="{Binding ActualValue}"