Listview / DataGrid返回{collection}

时间:2012-07-14 21:40:25

标签: c# wpf wpfdatagrid

在我的一个列中,我有一个List,我想在ListView或datagrid中显示。

该课程如下:

class Cab
{
    public Cab()
    {
        Stations = new List<string>();
    }

    public string Medication { get; set; }
    public List<string> Stations { get; set; }
}

以下是结果:

enter image description here

我尝试了几种不同的方法,但我不知道要查找哪个搜索词。我想做的是将电台格式化为看起来像:ER,ICU,Foo,每个med旁边的栏。

<ListView x:Name="myCab">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="140" Header="Medication" DisplayMemberBinding="{Binding Medication}" />
            <GridViewColumn Width="140" Header="Stations" DisplayMemberBinding="{Binding Stations}">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="140" Header="Column 3" />
        </GridView>
    </ListView.View>
</ListView>

2 个答案:

答案 0 :(得分:0)

您可以将班级更改为

class Cab
{
    public Cab()
    {
        Stations = new List<string>();
    }

    public string Medication { get; set; }
    public List<string> Stations { get; set; }

    public string StationsToString
    {
        set {}
        get { return string.Join(",", this.Stations.ToArray()); }
    }
}

然后将StationsToString设置为绑定列表的属性。

希望这有帮助。

此致

答案 1 :(得分:0)

我会使用ValueConverter

基本上第一步是创建实现ListToStringConverter

的类IValueConverter
using System.Windows.Data;
using System.Collections; 

namespace myNameSpace
{
   public class ListToStringConverter : IValueConverter
   {
      public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture) 
      {
         string returnValue = string.Empty; 
         List<string> targetList = value as List<string>; 

         foreach(string s in targetList)
         {
           if(!returnValue == string.Empty)
              returnValue += ", " + s; 
           else 
              returnValue += s; 
         }

         return returnValue; 
      }     

        public object ConvertBack(object value, Type targetType, 
    object parameter, System.Globalization.CultureInfo culture)
       {
          List<string> returnValue = new List<string>(); 

          string input = value as string; 
          returnValue.AddRange(input.Split(",")); 

          return returnValue; 
       }
   }
} 

然后在XAML中使用ValueConverter

  • 导入Namespacee
  • 在您的窗口中声明资源转换器
  • 修改绑定以使用值转换器

//假设您导入了命名空间

<Window>
<Window.Resources>
    <local:ListToStringConverter x:Key="listFormatter" /> 

</Window.Resources>

<ListView x:Name="myCab">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="140" Header="Medication" DisplayMemberBinding="{Binding Medication}" />
            <GridViewColumn Width="140" Header="Stations" DisplayMemberBinding="{Binding Path=Stations Converter={StaticResource listFormatter}}">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="140" Header="Column 3" />
        </GridView>
    </ListView.View>
</ListView>
</Window>

这样做的好处在于,您可以获得所需的结果,而不会使用UI特定属性污染您的类。