ListView 绑定 List<> 使双边修改同步

<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; //绑定变量
    }
}

人已赞赏
C#

C# 错误栈溢出异常(StackOverflowException)

2020-4-3 18:23:27

C#

C# 基础课程的完成

2020-4-6 3:56:38

各类网站开发

站长微信 710 342 444 或者扫一扫右侧二维码。
    0 条回复 A文章作者 M管理员
      暂无讨论,说说你的看法吧
    个人中心
    购物车
    优惠劵
    今日签到
    有新私信 私信列表
    搜索