我想简单地对我的自定义控件做一些数学运算。我需要取一个“宽度”值并将其分配给Converter Paramater。c#转换器获取指定的演员表无效
这里是绑定:
<Border x:Name="circleBorder"
Grid.Row="0"
CornerRadius="{Binding Path=ActualWidth, ElementName=circleGrid}"
Width="{Binding Path=ActualWidth, ElementName=circleGrid}"
Height="{Binding Path=ActualWidth, ElementName=circleGrid}"
BorderBrush="White"
BorderThickness="{Binding Converter={StaticResource CalculateBorder}, Path=Width, ElementName=circleBorder, ConverterParameter=30}">
这是转换器,它应该做一些简单的数学。
public class CalculateBorder : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((double)value/(double)parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我得到的return ((double)value/(double)parameter);
此错误:
指定的转换是无效的。
听起来好像你的'parameter'或'value'实际上可能并不是一个双。尝试使用'Convert.ToDouble()'而不是直接转换,因为它会正确处理更多的情况。 –
向我们展示CalculateBorder是什么。这是这里关键的缺失信息。最好的方法来证明这一点:不要在你的'Convert'方法中使用参数,只需将'(double)value'除以2.2。 – code4life
你应该在问之前调试这些东西。当您进行转换时,“value”和“parameter”中有什么? – Will