WPF 转换器 - 返回类类型的名称
我在列表中有一个列表..并且该列表的类型为“RequirementsBase”(抽象类)。
我想要做的是,当从父列表框中选择一个项目时,列表中每个项目的 Name 属性都会显示在列表框中。 (希望这是有道理的)。如果没有转换器,当列表框中显示项目时,我会获得该类的完整路径命名空间。一旦我连接了转换器,它就会显示列表的名称,而不是列表中的内容。它还在列表框中显示该名称,每个字母各占一行。
这是我连接转换器的 XAML:
<ListBox Name="lstRequirements"
ItemsSource="{Binding ElementName=listTreasureCards,
Path=SelectedItem.Requirements,
Converter={StaticResource RequirementConverter}}" />
这是我的转换器的代码:
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return value.GetType().Name;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return value.GetType();
}
我是菜鸟,所以请说菜鸟。 :)
I have a list inside of a list.. and this list is of type "RequirementsBase" (an abstract class).
What I want to do is have the Name property of each item in the list the display in a listbox when an item is selected from the parent list box. (Hopefully that makes sense). Without the converter, when items are displayed in the list box I get the full path namespace of the class. Once I hooked a converter up, it displayed the Name of the list, rather than what is in the list. It is also displaying that name with each letter on its own line within the list box.
Here is my XAML hooking up the converter:
<ListBox Name="lstRequirements"
ItemsSource="{Binding ElementName=listTreasureCards,
Path=SelectedItem.Requirements,
Converter={StaticResource RequirementConverter}}" />
Here is the code for my converter:
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return value.GetType().Name;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return value.GetType();
}
I am a noob, so please speak noob. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题是您将转换器放在列表上而不是列表元素上。为了解决这个问题,您必须将内容添加到项目模板(描述每个列表框项目如何显示的模板)并将转换器放在那里:
Your problem is that you put the converter on the list instead of list elements. In order to fix that, you have to add content to the item template (the template that describes how each list box item is shown) and put the converter there:
我相信你的意思是你有一个 IEnumerable>作为您的 listTreasureCards 数据源。那将是需求库列表的列表。
我相信您想要的是每个对象名称的平面列表(IEnumerable),其中每个元素对应于列表列表的元素。
如果是这种情况,您应该尝试如下所示的 LINQ 表达式。
因为这是一个loss-full算法,所以实现ConvertBack没有什么意义;只需抛出一个 NotImplementedException 即可。
另外,顺便说一句,字母显示在不同行的原因是 ItemSource 需要 IEnumerable。您的转换方法返回一个字符串,它恰好是一个 IEnumerable (字母字符串)。因此,由于这个原因,每个字母都会出现在不同的行中。
I believe what you're saying is you have an IEnumerable> as your listTreasureCards data source. That would be the list of list of RequirementsBase.
What I believe you want is a flat list of the names of each object (IEnumerable) where each element corresponds to an element of a list of list.
If this is the case, you should try a LINQ expression like the following.
Because this is a loss-full algorithm, there is little point in implementing ConvertBack; just throw a NotImplementedException.
Also, just as an aside, the reason that the letters are showing up on different lines is that the ItemSource expects an IEnumerable. Your convert method is returning a single String, which happens to be an IEnumerable (string of letters). So, for this reason, each letter ends up in a different line.
实现目标的最简单最直接的方法是重写 RequirementsBase.ToString :
The simplest most direct way of achieving your goal would be to override
RequirementsBase.ToString
: