Wpf 绑定列表<;字符串>;到GridViewColumn
我有一个Wpf 绑定列表<;字符串>;到GridViewColumn,wpf,data-binding,Wpf,Data Binding,我有一个GridView如下 <Grid> <ListView Margin="8,44,8,50" TabIndex="57" Name="CustomFieldList"> <ListView.View> <GridView AllowsColumnReorder="False"> <GridViewColumn Header="Semester Name"
GridView
如下
<Grid>
<ListView Margin="8,44,8,50" TabIndex="57" Name="CustomFieldList">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Header="Semester Name" Width="120" DisplayMemberBinding="{Binding FieldName}"/>
<GridViewColumn Header="Subjects" Width="150" DisplayMemberBinding="{Binding TypeValidations}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
问题学期
属性显示完美。但是主题
列表中没有。它显示为集合。如何解决这个问题 我建议您在视图模型上创建一个新的字符串属性,该属性将连接您的集合并返回该集合。这是最简单的方法
public class DigreePrograme
{
public string semester { get; set; }
public List<string> subjects { get; set; }
public string SubjectsDisplay
{
get
{
string returnVal = string.Empty;
foreach (string subject in subjects)
{
returnVal += subject + ", ";
}
return returnVal.TrimEnd(',', ' ');
}
}
}
公共类数字编程
{
公共字符串{get;set;}
公共列表主题{get;set;}
公共字符串主题显示
{
得到
{
string returnVal=string.Empty;
foreach(主题中的字符串主题)
{
returnVal+=主题+“,”;
}
返回returnVal.TrimEnd(',','');
}
}
}
类似的事情可能会有帮助
显然,也要更改绑定:)
干杯。
ste.您可以在
数据模板中的GridView列中添加项控件
,以显示所有主题
项
xaml:
代码:
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
Add(新的DigreeProgram{Serm=“Serm 1”,主题=新列表(新字符串[]{“Math”,“Art”,“Engish”});
}
私有ObservableCollection DigreList=新ObservableCollection();
公共可观察收集数字列表
{
获取{return digreeList;}
设置{digreeList=value;}
}
}
公共类数码节目
{
公共字符串{get;set;}
公共列表主题{get;set;}
}
结果:
那么您想在列单元格中显示列表吗?@sa_ddam213-是。你说得对,谢谢你的回复。如果我没有选择,我一定会选择这个。@NewDeveloper没问题,我会继续思考。。希望有什么东西会弹出:)它的工作。非常感谢你。我找了好几个小时。但并没有找到答案。另一个小问题。我们能把条目对齐到顶部吗。现在它位于中心。所以您希望它们水平而不是垂直排列。我希望对齐为VerticalContentAlignment=“Top”
Ok。修好了。这对我有帮助[
static ObservableCollection<DigreePrograme> digreeList = new ObservableCollection<DigreePrograme>();
public ObservableCollection<DigreePrograme> DigreeList
{
get { return digreeList }
set { digreeList = value; OnPropertyChanged("DigreeList"); }
}
CustomFieldList.ItemsSource=DigreeList;
public class DigreePrograme
{
public string semester { get; set; }
public List<string> subjects { get; set; }
public string SubjectsDisplay
{
get
{
string returnVal = string.Empty;
foreach (string subject in subjects)
{
returnVal += subject + ", ";
}
return returnVal.TrimEnd(',', ' ');
}
}
}
<Window x:Class="WpfApplication11.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="275" Width="275" x:Name="UI">
<Grid>
<ListView Name="CustomFieldList" ItemsSource="{Binding ElementName=UI, Path=DigreeList}" >
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Header="Semester Name" Width="120" DisplayMemberBinding="{Binding Semester}"/>
<GridViewColumn Header="Subjects" Width="150" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Subjects}" Width="150" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DigreeList.Add(new DigreePrograme { Semester = "Semester 1", Subjects = new List<string>(new string[] {"Math","Art","Engish" }) });
}
private ObservableCollection<DigreePrograme> digreeList = new ObservableCollection<DigreePrograme>();
public ObservableCollection<DigreePrograme> DigreeList
{
get { return digreeList; }
set { digreeList = value;}
}
}
public class DigreePrograme
{
public string Semester { get; set; }
public List<string> Subjects { get; set; }
}