<XALM> ListView代码段
<ListView x:Name="listViewAuthority" HorizontalAlignment="Left" Height="164" Margin="36,43,0,0" VerticalAlignment="Top" Width="340" Background="#CC0E0E0E" Foreground="White" BorderBrush="{x:Null}"> <ListView.View> <GridView> <GridViewColumn Width="50" Header="授权"> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel> <CheckBox IsChecked="{Binding Check}"/> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="权限描述" Width="200" DisplayMemberBinding="{Binding Path=Description}" /> <GridViewColumn Header="授权码" Width="50" DisplayMemberBinding="{Binding Path=AuthCode}" /> </GridView> </ListView.View> </ListView>
<c#> 类 Class (用于绑定的数据)
public class AuthGrid { private bool check; private string description; private int authCode; public bool Check { get { return check; } set { check = value; } } public string Description { get { return description; } set { description = value; } } public int AuthCode { get { return authCode; } set { authCode = value; } } /// <summary> /// 赋值函数 /// </summary> public AuthGrid(bool ck, string desc, int code) { this.check = ck; this.description = desc; this.authCode = code; } /// <summary> /// 初始化数据函数 /// </summary> public static List<AuthGrid> GetList() { var list = new List<AuthGrid>(); // 绑定显示到表格的数据 list.Add(new AuthGrid(false,"添加记录",1); list.Add(new AuthGrid(false, "查看任何记录", 5); list.Add(new AuthGrid(false, "编辑任何记录", 2); list.Add(new AuthGrid(false, "删除任何记录", 3); list.Add(new AuthGrid(false, "审核记录", 4); list.Add(new AuthGrid(false, "无限制搜索", 6); list.Add(new AuthGrid(false, "添加供应商", 20); list.Add(new AuthGrid(false, "编辑供应商", 21); list.Add(new AuthGrid(true, "查看月视图", 30); return list; } }
类中的3个变量: 用于XALM代码绑定到ListView
Check;
Description;
AuthCode;
public partial class Window1 : Window { private List<AuthGrid> AuthList= AuthGrid.GetList(); //加载数据到变量 /// <summary> /// 在窗口对象加载后设置绑定到变量 /// </summary> public Window1() { InitializeComponent(); listViewAuthority.ItemsSource=AuthList; //绑定变量 } }