Windows phone 7 为前台创建转换器
我创建了一个转换器,将前景绑定到一个特殊值并对其进行更改,但它始终将val设为null:Windows phone 7 为前台创建转换器,windows-phone-7,silverlight-4.0,Windows Phone 7,Silverlight 4.0,我创建了一个转换器,将前景绑定到一个特殊值并对其进行更改,但它始终将val设为null: public class PositionConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { //string vall;
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>
公共类位置转换器:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
//弦谷;
//TextBlock txt=TextBlock.TextProperty(
var val=作为文本块的值;
如果(val!=null)
{
if(val.Text.StartsWith(“-”)
{
返回新的SolidColorBrush(颜色为红色);
}
其他的
{
返回新的SolidColorBrush(颜色为绿色);
}
}
返回新的SolidColorBrush(颜色为红色);
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
value
是绑定中涉及的值(在您的案例中为amount),而不是控件。因此,将其强制转换为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();
}
}
value
是绑定中涉及的值(在您的案例中为:amount),而不是控件。因此,将其强制转换为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();
}
}