我有一个简单的 Observable 集合,它有两个公共属性,int ID 和 List Targets。代码隐藏看起来像(简化代码以删除不必要且不相关的代码):
public class MyClass
{
public ObservableCollection<SomeClass> jobs;
public class SomeClass
{
private int id;
private List<string> targets;
public int ID
{
get { return id; }
set { id = value; }
}
public List<string> Targets
{
get { return targets; }
set { targets = value; }
}
public SomeClass(int _id, List<string> _targets)
{
id = _id;
targets = _targets;
}
}
public MyClass()
{
InitializeComponent();
jobs = new ObservableCollection<SomeClass>();
myListView.ItemsSource = jobs; //jobs is populated from a a loader in Window_Loaded
}
}
xaml 中的 ListView 和绑定如下所示:
<ListView Name="MyListView" ItemsSource="{Binding Path=jobs, RelativeSource={RelativeSource AncestorType=Window},
Mode=OneWay}" Width="480" Height="155" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,35,10,0" >
<ListView.ContextMenu>
<ContextMenu Name="contextMenuJobRemove">
<ContextMenu.BitmapEffect>
<OuterGlowBitmapEffect />
</ContextMenu.BitmapEffect>
<MenuItem Header="Remove" Click="contextMenuJobRemove_Click" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Parent}"/>
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Broadcast call targets">
<GridViewColumn DisplayMemberBinding="{Binding Path=ID}" Header="ID" Width="50" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Targets}" Header="Targets" Width="100" />
</GridView>
</ListView.View>
</ListView>
因此,当 ListView 显示时,Targets 列正确地显示“(集合)”。理想情况下,我希望此列显示类似String.Join(",", Targets.ToArray())
. 如何做到这一点,我是在 xaml 还是代码隐藏中做到这一点?