用转换器绑定字符串集合

时间:2015-03-12 21:09:39

标签: wpf binding

我有下一个代码:

class MyViewModel 
{
    public List<string> Items {get;set;}
}

和xaml

<ItemsControl ItemsSource="{Binding Items}">
  <ItemTemplate>
     <TextBlock Text="{Binding}" />
 </ItemTemplate>

完美无缺。 现在我需要添加一些转换:

 <ItemsControl ItemsSource="{Binding Items}">
  <ItemTemplate>
     <TextBlock Text="{Binding, Converter={StaticResource WorldBestConverter}}" />
 </ItemTemplate>   

它不起作用。我可以将Items属性更改为List&lt; someObj中&GT;其中SomeObj是包含类的属性值并使用下一个方法:

<ItemsControl ItemsSource="{Binding Items}">
  <ItemTemplate>
     <TextBlock Text="{Binding Value, Converter={StaticResource WorldBestConverter}}" />
 </ItemTemplate>  

但我不想为这种情况介绍新课程。 如何通过转换绑定到当前列表?

1 个答案:

答案 0 :(得分:1)

你可以添加。用于绑定。请参阅以下代码。

 <ItemsControl ItemsSource="{Binding Items}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding .,Converter={StaticResource myConv}}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
相关问题