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

时间:2017-10-10 17:52:11

标签: c# wpf ivalueconverter

我一直在尝试实现一个简单的值转换器,我无法弄清楚为什么没有应用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>

视图模型

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");
    }
}

ValueConverter

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();
    }
}

1 个答案:

答案 0 :(得分:3)

设置

Text="{Binding ActualValue}"
直接在TextBlock上的

之类的Style Setter高value precedence
<Setter Property="Text" Value="{Binding StringFormat='({0:+0.0})'}"/>

所以来自Setter的Bindings永远不会被应用。它们也写错了,因为它们错过了属性路径ActualValue

你的方法不起作用。您应该使用带有Bindings的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}"