我试图让从DependencyObject
继承的自定义转换器,但它不工作:绑定在转换器?
转换器:
public class BindingConverter : DependencyObject , IValueConverter
{
public object Value
{
get { return (object)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(object), typeof(BindingConverter), new PropertyMetadata(null));
public object Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture)
{
Debug.Assert(Value != null); //fails
return Value;
}
public object ConvertBack(object value, Type targetType, object parameter, Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
的XAML:
<StackPanel x:Name="this">
<!--works-->
<ContentControl Content="{Binding ActualHeight, ElementName=this}"/>
<!--doesn't work-->
<ContentControl>
<Binding>
<Binding.Converter>
<BindingConverter Value="{Binding ActualHeight, ElementName=this}" />
</Binding.Converter>
</Binding>
</ContentControl>
<TextBlock Text="{Binding Animals}"/>
</StackPanel>
我错过了什么?
首先,遗憾用于选择的ActualHeight /宽度为我的例子。在我真实世界的例子中,我试图实现一个模仿WPF'{RelativeSourcePreviuosData}'功能的转换器。所以要做到这一点,我想传递一下绑定,这是对集合的引用。我试图用'{Binding Converter = {PreviousDataConverter Collection = {Binding ItemsSource,ElementName = myItemsControl}}}'绑定。 – Shimmy 2012-03-29 00:49:34
我想我现在得到了这个问题,你试图在UI上一起显示源代码的当前和以前的数据。如果我是对的,你可以使用Converter参数来实现这个功能。我以前不需要这个特性,但对于某些转换器,我将参数传递给Convert方法以获得不同的响应。 {Binding Source = itemssouce,converter = preconverter,converterparameter = {Binding ElementName = X,Path = index?},...这样的事情? – 2012-03-29 07:37:31
这肯定不会奏效,'Parameter'转换器不可绑定AFAIK。 – Shimmy 2012-03-29 15:28:21