wpf 动态添生成Expander
从数据库读取数据,班级分类为Expander.Header,班级为Expander.Content,请说的详细点,本人刚接触Expander。...
从数据库读取数据,班级分类为Expander.Header, 班级为Expander.Content ,请说的详细点,本人刚接触Expander。
展开
1个回答
展开全部
写了个demo给你,希望能帮到你。
首先是两个类。
public class TestClass
{
public int ClassTypeId { get; set; }
public List<TestClassRoom> ClassRooms { get; set; }
}
public class TestClassRoom
{
public string classroomId { get; set; }
}
后台代码:
List<TestClass> classlist;
public ToolTipTestWindow()
{
InitializeComponent();
LoadData();
Expander ep;
foreach (TestClass ts in classlist)
{
ep = new Expander();
ep.Header = ts.ClassTypeId;
ep.Content = new ListBox()
{
ItemsSource=ts.ClassRooms,
DisplayMemberPath = "classroomId"
};
sp.Children.Add(ep);
}
}
private void LoadData()
{
classlist = new List<TestClass>();
TestClass class1 = new TestClass();
class1.ClassTypeId = 1;
List<TestClassRoom> list1 = new List<TestClassRoom>()
{
new TestClassRoom(){classroomId="1a"},
new TestClassRoom(){classroomId="1b"},
new TestClassRoom(){classroomId="1c"},
};
class1.ClassRooms = list1;
classlist.Add(class1);
TestClass class2 = new TestClass();
class2.ClassTypeId = 2;
List<TestClassRoom> list2 = new List<TestClassRoom>()
{
new TestClassRoom(){classroomId="2a"},
new TestClassRoom(){classroomId="2b"},
new TestClassRoom(){classroomId="2c"},
};
class2.ClassRooms = list2;
classlist.Add(class2);
}
前台代码:
<StackPanel x:Name="sp"></StackPanel>
首先是两个类。
public class TestClass
{
public int ClassTypeId { get; set; }
public List<TestClassRoom> ClassRooms { get; set; }
}
public class TestClassRoom
{
public string classroomId { get; set; }
}
后台代码:
List<TestClass> classlist;
public ToolTipTestWindow()
{
InitializeComponent();
LoadData();
Expander ep;
foreach (TestClass ts in classlist)
{
ep = new Expander();
ep.Header = ts.ClassTypeId;
ep.Content = new ListBox()
{
ItemsSource=ts.ClassRooms,
DisplayMemberPath = "classroomId"
};
sp.Children.Add(ep);
}
}
private void LoadData()
{
classlist = new List<TestClass>();
TestClass class1 = new TestClass();
class1.ClassTypeId = 1;
List<TestClassRoom> list1 = new List<TestClassRoom>()
{
new TestClassRoom(){classroomId="1a"},
new TestClassRoom(){classroomId="1b"},
new TestClassRoom(){classroomId="1c"},
};
class1.ClassRooms = list1;
classlist.Add(class1);
TestClass class2 = new TestClass();
class2.ClassTypeId = 2;
List<TestClassRoom> list2 = new List<TestClassRoom>()
{
new TestClassRoom(){classroomId="2a"},
new TestClassRoom(){classroomId="2b"},
new TestClassRoom(){classroomId="2c"},
};
class2.ClassRooms = list2;
classlist.Add(class2);
}
前台代码:
<StackPanel x:Name="sp"></StackPanel>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询