【问题标题】:Checkbox column not updating when checked选中时复选框列未更新
【发布时间】:2019-02-02 18:29:33
【问题描述】:

代码如下:

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
        }
    }

所以我遇到的问题是我似乎无法检查多个复选框。一旦我尝试选中第二个复选框,之前选中的复选框就会变为未选中状态。

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

【问题讨论】:

  • 根据tagging,我已从您的问题标题中删除了您的 C# 和 WPF 标签。
  • 您能描述一下如何重现您的问题吗?我在您提供的代码中没有看到它。
  • 看看你对应的xaml代码会很有帮助
  • @dontbyteme: 还有CellClick()内部发生的事情

标签: c# wpf checkbox datagrid


【解决方案1】:

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

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

如果为名称传入 null 或空字符串 (""),则默认名称 ("Column1", “Column2”等)被赋予该列。 MS Docs

用空格作为列名似乎有问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-12
    • 2022-01-12
    • 2018-10-10
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    相关资源
    最近更新 更多