我正在尝试制作一个从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>我错过了什么吗?
发布于 2012-03-30 01:21:48
我在我的项目中有一些地方需要类似的功能。我不能给你看确切的样本,只是一个想法:
公共类BindingHelper : FrameworkElement
将其他控件的输出属性更改为BindingHelper类的属性
发布于 2012-03-28 22:39:42
注意!ActualHeight属性的绑定在绑定时有错误!
为什么在编写转换器时继承DependencyObject?您应该只实现IValueConverter。
试试看
首先通过"MyConverterResource“键在您的资源上添加MyConverter,然后可以通过以下方式在xaml端或cs端执行此操作:
//You may do it on XAML side <UserControl.Resources>...
this.Resources.Add("MyConverterResource",new MyConverter());
<TextBlock Text="{Binding ActualHeight,ElementName=this
,Converter=MyConverterResource}"/>
public class MyConverter: IValueConverter
{
public object Convert(object value, Type targetType
, object parameter,Globalization.CultureInfo culture)
{
return "Your Height is:"+Value.toString();
}
}希望有帮助
发布于 2012-03-28 22:47:27
在Silverlight中,ActualHeight和ActualWidth属性不会在属性更新时发出通知。因此,绑定到它们将不起作用。
https://stackoverflow.com/questions/9900097
复制相似问题