2

I want to convert the items of a listview with the help of an property in the ViewModel. I didn't get this to work... the column with the converter is empty the convert method is never reached. in the output window is following message:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=ObjectDefinition; DataItem=null; target element is 'OsGuidToNameConverter' (HashCode=66244779); target property is 'BindableConverterParameter' (type 'XmlDocument')

the ObjectDefinition is filled at runtime after this message appears

What is the best practise to do that

xaml

<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>

Converter

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 "";
    }
}
masterchris_99
  • 2,683
  • 7
  • 34
  • 55

1 Answers1

1

Your problem is in the error message

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=ObjectDefinition; DataItem=null; target element is 'OsGuidToNameConverter' (HashCode=66244779); target property is 'BindableConverterParameter' (type 'XmlDocument')

The DataContext (DataItem) behind your Converter is null, so the property "ObjectDefinition" cannot be found.

Try moving your converter to <GridViewColumn.Resources> so it has the correct DataContext for the binding, or switch to an IMultiValueConverter which lets you pass in multiple bound values.

A third alternative would also be to pass the entire data item to the Converter using {Binding }, then cast it to your data type and you can access the Value and ObjectDefinition properties of it to perform the conversion.

Rachel
  • 130,264
  • 66
  • 304
  • 490