2012-08-16 69 views
4

我正在使用一个需要获取类型列表的值转换器,这是转换器的一个属性。如果我会用双值列表,我可以使用下面的语法(其中工作正常):在XAML中声明类型列表

代码

public class MyConverter : IValueConverter 
{ 
    public List<double> MyList { get; set; } 

    // ... 
} 

XAML

<Converter:MyConverter x:Key="MyConverter"> 
    <Converter:MyConverter.MyList> 
     <System.Double>1</System.Double> 
     <System.Double>2</System.Double> 
    </Converter:MyConverter.MyList> 
</Converter:MyConverter> 

但是,如果我尝试将这种方法与类型列表一起使用,抛出异常:

Object of type 'System.RuntimeType' cannot be converted to type 'System.Collections.Generic.List`1[System.Type]' 

这是我的转换器和其用法:

代码

public class MyConverter : IValueConverter 
{ 
    public List<Type> MyList { get; set; } 

    // ... 
} 

XAML

<Converter:MyConverter x:Key="MyConverter"> 
    <Converter:MyConverter.MyList> 
     <x:Type TypeName="MyType1" /> 
     <x:Type TypeName="MyType2" /> 
    </Converter:MyConverter.MyList> 
</Converter:MyConverter> 

我猜XAML语法是错误的,但我无法找到合适的句法。

+0

做了任何答案适合你? – Artiom 2012-08-16 12:20:41

+0

@Artiom你的回答很有帮助,谢谢。无论如何,在我决定什么是令人满意的答案之前,我想更多地了解这个问题。 – MatthiasG 2012-08-16 13:42:25

+0

你有没有尝试过的解决方案?我已经更新了我的** colinsmith **的解决方案似乎也可以工作 – Artiom 2012-08-17 09:21:35

回答

2

好像在XAML设计错误。给定的波纹管代码为我工作。我可以构建和运行应用程序。但在设计器R·hightlights两条线系统:类型和设计者崩溃,每行接下来的两个错误:

错误1种类型“类型”是不能用作一个对象元素,因为它是 不公开或未定义公共无参数构造函数或 类型转换器。
错误2“类型”类型不支持直接 内容。 因此,当我在尝试解决方案之前(在给出以前的解决方案之前)我以为我做错了。 但编译器仍然不会在编译时给出任何错误。反正在这里它的外观:

<Window.Resources> 
    <local:Holder x:Key="one"> 
     <local:Holder.Types> 
      <System:Type>System:Int32</System:Type> 
      <System:Type>System:Double</System:Type> 
     </local:Holder.Types> 
    </local:Holder> 
</Window.Resources> 
<Grid > 
    <ListBox DataContext="{StaticResource one}" ItemsSource="{Binding Path=Types, Converter={StaticResource one}}" /> 
</Grid> 

至于你所看到的,所不同的是在声明。你必须使用System.Type而不是x:Type。

和代码作为样品

using System; 
using System.Collections.Generic; 
using System.Globalization; 
using System.Windows.Data; 
using System.Linq; 

namespace stackProjects 
{ 
    public class Holder : IValueConverter 
    { 
     public List<Type> Types { get; set; } 

     public Holder() 
     { 
      Types=new List<Type>(); 
     } 

     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return Types.Select(x => x.Name).ToList(); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

正如我说,这是因为类型类是抽象。 希望它有帮助

+0

感谢您的回答。尽管我不喜欢放弃泛型,但我暂时不会使用它们。我只是想知道为什么泛型在使用double值列表时没有问题。 – MatthiasG 2012-08-16 13:13:03

+0

我已经更新了我的答案 – Artiom 2012-08-17 02:00:38

1

确定下面的例子编译并运行....我看到转换器被调用和列表填充2 Type对象。可能有更好的方法来做到这一点。


下面是完整的代码,我用:

namespace WpfApplication4 
{ 
    public class MyConverter : IValueConverter 
    { 
     public IList<Type> MyList { get; set; } 

     public MyConverter() 
     { 
      MyList = new List<Type>(); 
     } 

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      return null; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public class MyType 
    { 
     public string Name { get; set; } 

     public MyType() 
     { 

     } 
    } 
} 

<Window x:Class="WpfApplication4.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication4" 
     xmlns:runtime="clr-namespace:System.Runtime.InteropServices;assembly=mscorlib" 
     xmlns:sys="clr-namespace:System;assembly=mscorlib" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <sys:String x:Key="testdata">TestData</sys:String> 
     <x:Array x:Key="listoftypes" Type="{x:Type sys:Type}"> 
      <x:Type Type="local:MyType"/> 
      <x:Type Type="local:MyType"/> 
     </x:Array> 
     <local:MyConverter x:Key="myconv" MyList="{StaticResource listoftypes}"/> 
    </Window.Resources> 
    <Grid> 
     <TextBlock Text="{Binding Source={StaticResource testdata}, Converter={StaticResource myconv}}"/> 
    </Grid> 
</Window> 
+1

也许我应该澄清这个问题。双列表转换器正在工作,并被认为表明它将如何正常工作。它只是不适用于类型列表。 – MatthiasG 2012-08-16 09:38:53