我想借助 ViewModel 中的属性转换列表视图的项目。我没有让这个工作......带有转换器的列是空的,转换方法永远不会到达。在输出窗口中是以下消息:
System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。绑定表达式:路径=对象定义;数据项=空;目标元素是“OsGuidToNameConverter”(HashCode=66244779);目标属性是“BindableConverterParameter”(类型“XmlDocument”)
出现此消息后,在运行时填充 ObjectDefinition
这样做的最佳做法是什么
xml
<Window.Resources>
<ResourceDictionary>
<local:OsGuidToNameConverter x:Key="formatter" BindableConverterParameter="{Binding Path=ObjectDefinition}" />
</ResourceDictionary>
</Window.Resources>
<ListView Grid.Row="2" Name="listView1" ItemsSource="{Binding Path=Config.DocumentTypes}">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Objekttyp" DisplayMemberBinding="{Binding ObjektTyp}" />
<GridViewColumn Width="140" Header="Bezeichnung">
<GridViewColumn.DisplayMemberBinding>
<Binding Path="Value" Converter="{StaticResource formatter}" />
</GridViewColumn.DisplayMemberBinding>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
转换器
public class OsGuidToNameConverter : DependencyObject, IValueConverter
{
public static DependencyProperty BindableConverterParameterProperty =
DependencyProperty.Register("BindableConverterParameter", typeof(XmlDocument),
typeof(OsGuidToNameConverter));
public XmlDocument BindableConverterParameter
{
get { return (XmlDocument)GetValue(BindableConverterParameterProperty); }
set { SetValue(BindableConverterParameterProperty, value); }
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (BindableConverterParameter is XmlDocument)
{
try
{
FieldParameter definitionForField = (FieldParameter)ObjectDefinitionHelper.GetObjectDefinitionByAttribute((string)value, SuchFeldTypes.osguid, BindableConverterParameter, SuchFeldTypes.name);
return definitionForField.Value;
}
catch (Exception e)
{
Console.WriteLine(e);
return "";
}
}
else
{
return "";
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return "";
}
}