选中时,复选框列未更新

时间:2018-08-28 07:37:49

标签: c# wpf checkbox datagrid

代码如下:

Xaml:

<UserControl x:Class="Prototype.Forms.UserForm"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Prototype.Forms"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="400">
    <Grid>
        <DataGrid Name="MainGrid">
        </DataGrid>
    </Grid>
</UserControl>

C#:

public partial class UserForm : UserControl
    {

        public UserForm(IList <Element> list)
        {
            InitializeComponent();

            DataTable dt = new DataTable();

            dt.Columns.Add(" ",typeof(bool));
            dt.Columns.Add("Iteration");
            dt.Columns.Add("View name");
            dt.Columns.Add("Type");

            for (int i = 0; i < list.Count; i++)
            {
                DataRow r = dt.NewRow();

                r[1] = i.ToString();
                r[2] = list[i].Name;
                r[3] = "some element type";

                dt.Rows.Add(r);
            }

            MainGrid.ItemsSource = dt.DefaultView;
            MainGrid.MouseLeftButtonUp += new MouseButtonEventHandler(CellClick);
        }

        private void CellClick(object sender, EventArgs e)
        {
            //Do stuff
        }
    }

所以我遇到的问题是我似乎无法选中多个复选框。当我尝试再次选中第二个复选框时,先前选中的复选框将变为未选中状态。

鼠标按钮事件是尝试使复选框保持选中状态的尝试失败,但失败。

1 个答案:

答案 0 :(得分:0)

删除空格或为复选框列添加列名:

dt.Columns.Add("ColumnName", typeof(bool));
  

如果为该名称传入null或空字符串(“”),则使用默认名称(“ Column1”,   该列将被赋予“ Column2”,依此类推。 MS Docs

列名的空白似乎存在问题。

相关问题