1

前景を特別な値にバインドして変更するコンバーターを作成しましたが、常に val を null として使用します。

public class PositionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //string vall;
        //TextBlock txt= TextBlock.TextProperty(

        var val = value as TextBlock;
        if (val != null)
        {
            if (val.Text.StartsWith("-"))
            {
                return new SolidColorBrush(Colors.Red);

            }
            else
            {
                return new SolidColorBrush(Colors.Green);
            }
        }
        return new SolidColorBrush(Colors.Red);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
           <TextBlock FontSize="28"  x:Name="solde" TextWrapping="Wrap"  >
           <Run Text="        Solde : " Foreground="Black"/>
           <Run Text="{Binding amount}" Foreground="{Binding amount, Converter=               {StaticResource PositionConverter}}" Language="fr-FR"/>
             </TextBlock>
4

1 に答える 1

1

valueコントロールではなく、バインディングに含まれる値(あなたの場合は金額)です。したがって、TextBlock へのキャストは機能しません。

代わりにこれを試すことができます:

public class PositionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {          
        if (value == null)
        {
            return new SolidColorBrush(Colors.Red);
        }

        if (value.ToString().StartsWith("-"))
        {
            return new SolidColorBrush(Colors.Red);                   
        }

        return new SolidColorBrush(Colors.Green);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
于 2012-05-18T09:15:50.033 に答える