[英]wpf, register DP, bindable converter parameter
[ValueConversion(typeof(object), typeof(object))]
public class BindableConvertor : DependencyObject, IValueConverter
{
public object BindableParameter
{
get { return GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
nameof(BindableParameter),
typeof(object),
typeof(BindableConvertor),
new PropertyMetadata(String.Empty)
);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// actions here...
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<Application.Resources>
<local:BindableConvertor x:Key="MyConvertor" BindableParameter="{Binding AnyTargetProperty}" />
</Application.Resources>
最后:
<ListBox Name="ViewBox"
Grid.Row="0"
DisplayMemberPath="Value"
ItemsSource="{Binding SomePropertyFromWindowDataContext,
Converter={StaticResource MyConvertor}}" />
結果:System.Windows.Data錯誤:2:找不到目標元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:路徑=; 的DataItem = NULL; 目標元素是'BindableConvertor'(HashCode = 19986012); 目標屬性是“ BindableParameter”(類型“ Object”)。 而且我的“ BindableParameter”始終等於默認值(空)。
但是,如果我做類似的事情:
<local:BindableConvertor x:Key="MyConvertor" BindableParameter="Constant text here..." />
...那么它完美地工作了。
有什么想法嗎?
可能是因為您的轉換器繼承自DependencyObject。 請嘗試使用Freezable。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.