[英]XAML Binding to a converter
what I am trying to do is relatively simple. 我想做的是相对简单。 I am just trying to bind the Y element of a TranslateTransform on an ellipse to 1/2 the height of the ellipse:
我只是试图将椭圆上的TranslateTransform的Y元素绑定到椭圆的高度的1/2:
<Ellipse Name="EllipseOnlyLFA" Height="200" Fill="Yellow" HorizontalAlignment="Left" VerticalAlignment="Bottom" ClipToBounds="True">
<Ellipse.Width>
<Binding ElementName="EllipseOnlyLFA" Path="Height"/>
</Ellipse.Width>
<Ellipse.RenderTransform>
<TranslateTransform>
<TranslateTransform.Y>
<Binding Converter="MultiplyByFactor" ElementName="EllipseOnlyLFA" Path="Height" ConverterParameter="0.5"/>
</TranslateTransform.Y>
</TranslateTransform>
</Ellipse.RenderTransform>
</Ellipse>
I also have the following converter: 我也有以下转换器:
public class MultiplyByFactor : 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)
{
return true;
}
}
I am getting an error on the XAML line where I actually use the converter. 我在实际使用转换器的XAML线路上收到错误。 The error is
错误是
'Set property 'System.Windows.Data.Binding.Converter' threw an exception.'
'set property'System.Windows.Data.Binding.Converter'抛出异常。' Line number '22' and line position '8'.
行号'22'和行位置'8'。
Can anyone shed some light on how to do this? 任何人都可以阐明如何做到这一点? EDIT: Yes, I have the converter added as a resource.
编辑:是的,我已将转换器添加为资源。
You need to add the converter to the resources 您需要将转换器添加到资源
Edit 编辑
You need to add the namespace too 您还需要添加命名空间
xmlns:c="clr-namespace:WpfApplication1"
end edit 结束编辑
<Window.Resources>
<c:MultiplyByFactor x:Key="myMultiplyByFactor"/>
</Window.Resources>
Then you can use the instance from the resources 然后,您可以使用资源中的实例
<TranslateTransform.Y>
<Binding Converter={StaticResource myMultiplyByFactor}"
ElementName="EllipseOnlyLFA"
Path="Height" ConverterParameter="0.5"/>
</TranslateTransform.Y>
There are 2 thing wrong with your code 您的代码有两个问题
1) your converter needs to be accessed using the StaticResource
declaration 1)需要使用
StaticResource
声明访问您的转换器
<Binding Converter="{StaticResource myMultiplyByFactor}"
ElementName="EllipseOnlyLFA" Path="Height" ConverterParameter="0.5"/
2) Your converter parameter is a string by default, so you need to convert it to a double 2)默认情况下,您的converter参数是一个字符串,因此您需要将其转换为double
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
double.TryParse((parameter as string).Replace(',', '.'), NumberStyles.Any, CultureInfo.InvariantCulture, out double param);
return param * (double)value;
}
The Parameter probably gets passed as a String. 参数可能作为String传递。 Set a breakpoint in your Converter and look at the values of
value
and parameter
. 在Converter中设置断点并查看
value
和parameter
的value
。 You might need to use double.Parse instead of the cast. 您可能需要使用double.Parse而不是强制转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.