简体   繁体   English

WPF转换器,带有ViewModel中的参数

[英]WPF Converter with parameter from ViewModel

I want to convert the items of a listview with the help of an property in the ViewModel. 我想借助ViewModel中的属性来转换listview的项目。 I didn't get this to work... the column with the converter is empty the convert method is never reached. 我没有使它起作用...转换器所在的列为空,从来没有达到convert方法。 in the output window is following message: 在输出窗口中显示以下消息:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:Path=ObjectDefinition; BindingExpression:Path = ObjectDefinition; DataItem=null; 的DataItem = NULL; target element is 'OsGuidToNameConverter' (HashCode=66244779); 目标元素是“ OsGuidToNameConverter”(HashCode = 66244779); target property is 'BindableConverterParameter' (type 'XmlDocument') 目标属性是“ BindableConverterParameter”(类型“ XmlDocument”)

the ObjectDefinition is filled at runtime after this message appears 出现此消息后,将在运行时填充ObjectDefinition

What is the best practise to do that 最佳做法是什么

xaml 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 "";
    }
}

Your problem is in the error message 您的问题出在错误消息中

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:Path=ObjectDefinition; BindingExpression:Path = ObjectDefinition; DataItem=null ; DataItem = null ; target element is 'OsGuidToNameConverter' (HashCode=66244779); 目标元素是“ OsGuidToNameConverter”(HashCode = 66244779); target property is 'BindableConverterParameter' (type 'XmlDocument') 目标属性是“ BindableConverterParameter”(类型“ XmlDocument”)

The DataContext ( DataItem ) behind your Converter is null , so the property "ObjectDefinition" cannot be found. Converter后面的DataContextDataItem )为null ,因此找不到属性"ObjectDefinition"

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. 尝试将转换器移动到<GridViewColumn.Resources>以便它具有用于绑定的正确DataContext ,或切换到IMul​​tiValueConverter ,该转换器可让您传递多个绑定值。

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. 第三种替代方法是使用{Binding }将整个数据项传递给Converter ,然后将其ObjectDefinition转换为您的数据类型,然后您可以访问其ValueObjectDefinition属性来执行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM